【LEDE】x86软路由之路-07-GCC编译失败(glibc2.26)

架构x86_64

在使用OpenWrt SDK编译gcc package的时候,报如下错误:

i486-openwrt-linux-gnu-gcc -g -O2 -g -Os -O2 -g -O2 -g -Os -DIN_GCC -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -isystem ./include -fpic -mlong-double-80 -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector -fpic -mlong-double-80 -I. -I. -I../../host-i486-openwrt-linux-gnu/gcc -I../.././libgcc -I../.././libgcc/. -I../.././libgcc/../gcc -I../.././libgcc/../include -DHAVE_CC_TLS -DUSE_TLS -o unwind-dw2.o -MT unwind-dw2.o -MD -MP -MF unwind-dw2.dep -fexceptions -c ../.././libgcc/unwind-dw2.c -fvisibility=hidden -DHIDE_EXPORTS
cc1: note: someone does not honour COPTS correctly, passed 0 times
In file included from ../.././libgcc/unwind-dw2.c:401:0:
./md-unwind-support.h: In function 'x86_fallback_frame_state':
./md-unwind-support.h:141:18: error: field 'uc' has incomplete type
struct ucontext uc;
^~
../.././libgcc/shared-object.mk:14: recipe for target 'unwind-dw2.o' failed
make[5]: *** [unwind-dw2.o] Error 1
make[5]: Leaving directory '/home/wang/Desktop/openwrt-x86/build_dir/target-i386_pentium4_glibc/gcc-5.4.0/i486-openwrt-linux-gnu/libgcc'
Makefile:12262: recipe for target 'all-target-libgcc' failed
make[4]: *** [all-target-libgcc] Error 2
make[4]: Leaving directory '/home/wang/Desktop/openwrt-x86/build_dir/target-i386_pentium4_glibc/gcc-5.4.0'
Makefile:879: recipe for target 'all' failed
make[3]: *** [all] Error 2
make[3]: Leaving directory '/home/wang/Desktop/openwrt-x86/build_dir/target-i386_pentium4_glibc/gcc-5.4.0'
Makefile:176: recipe for target '/home/wang/Desktop/openwrt-x86/build_dir/target-i386_pentium4_glibc/gcc-5.4.0/.built' failed
make[2]: *** [/home/wang/Desktop/openwrt-x86/build_dir/target-i386_pentium4_glibc/gcc-5.4.0/.built] Error 2
make[2]: Leaving directory '/home/wang/Desktop/openwrt-x86/feeds/packages/devel/gcc'
Command exited with non-zero status 2
time: package/feeds/packages/gcc/compile#1.49#1.08#19.61
package/Makefile:107: recipe for target 'package/feeds/packages/gcc/compile' failed
make[1]: *** [package/feeds/packages/gcc/compile] Error 2
make[1]: Leaving directory '/home/wang/Desktop/openwrt-x86'
/home/wang/Desktop/openwrt-x86/include/toplevel.mk:216: recipe for target 'package/gcc/compile' failed
make: *** [package/gcc/compile] Error 2
wang@wang-VirtualBox:~/Desktop/openwrt-x86$

在这里找到灵感:https://github.com/voidlinux/void-packages/issues/7284

解决方案:

修改{openwrt-ROOT}/build_dir/target-x86_64-glibc/gcc-5.4.0/x86_64-openwrt-linux-gnu/libgcc/md-unwind-support.h中的俩处:

Fix1:@line 61:
if (*(unsigned char *)(pc+0) == 0x48
&& *(unsigned long long *)(pc+1) == RT_SIGRETURN_SYSCALL)
{
// struct ucontext *uc_ = context->cfa;
ucontext_t uc_ = context->cfa;
/
 The void * cast is necessary to avoid an aliasing warning.
The aliasing warning is correct, but should not be a problem
because it does not alias anything. */
sc = (struct sigcontext *) (void *) &uc_->uc_mcontext;
}
Fix2:@line144
{
struct rt_sigframe {
int sig;
siginfo_t *pinfo;
void *puc;
siginfo_t info;
// struct ucontext uc;
ucontext_t uc;
} *rt_ = context->cfa;

最终效果:

/* DWARF2 EH unwinding support for AMD x86-64 and x86.
   Copyright (C) 2004-2015 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */

/* Do code reading to identify a signal frame, and set the frame
   state data appropriately.  See unwind-dw2.c for the structs.
   Don't use this at all if inhibit_libc is used.  */

#ifndef inhibit_libc

/* There's no sys/ucontext.h for glibc 2.0, so no
   signal-turned-exceptions for them.  There's also no configure-run for
   the target, so we can't check on (e.g.) HAVE_SYS_UCONTEXT_H.  Using the
   target libc version macro should be enough.  */
#if defined __GLIBC__ && !(__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)

#include <signal.h>
#include <sys/ucontext.h>

#ifdef __x86_64__

#define MD_FALLBACK_FRAME_STATE_FOR x86_64_fallback_frame_state

static _Unwind_Reason_Code
x86_64_fallback_frame_state (struct _Unwind_Context *context,
			     _Unwind_FrameState *fs)
{
  unsigned char *pc = context->ra;
  struct sigcontext *sc;
  long new_cfa;

  /* movq $__NR_rt_sigreturn, %rax ; syscall.  */
#ifdef __LP64__
#define RT_SIGRETURN_SYSCALL	0x050f0000000fc0c7ULL
#else
#define RT_SIGRETURN_SYSCALL	0x050f40000201c0c7ULL
#endif
  if (*(unsigned char *)(pc+0) == 0x48
      && *(unsigned long long *)(pc+1) == RT_SIGRETURN_SYSCALL)
    {
      // struct ucontext *uc_ = context->cfa;
	ucontext_t *uc_ = context->cfa;
      /* The void * cast is necessary to avoid an aliasing warning.
         The aliasing warning is correct, but should not be a problem
         because it does not alias anything.  */
      sc = (struct sigcontext *) (void *) &uc_->uc_mcontext;
    }
  else
    return _URC_END_OF_STACK;

  new_cfa = sc->rsp;
  fs->regs.cfa_how = CFA_REG_OFFSET;
  /* Register 7 is rsp  */
  fs->regs.cfa_reg = 7;
  fs->regs.cfa_offset = new_cfa - (long) context->cfa;

  /* The SVR4 register numbering macros aren't usable in libgcc.  */
  fs->regs.reg[0].how = REG_SAVED_OFFSET;
  fs->regs.reg[0].loc.offset = (long)&sc->rax - new_cfa;
  fs->regs.reg[1].how = REG_SAVED_OFFSET;
  fs->regs.reg[1].loc.offset = (long)&sc->rdx - new_cfa;
  fs->regs.reg[2].how = REG_SAVED_OFFSET;
  fs->regs.reg[2].loc.offset = (long)&sc->rcx - new_cfa;
  fs->regs.reg[3].how = REG_SAVED_OFFSET;
  fs->regs.reg[3].loc.offset = (long)&sc->rbx - new_cfa;
  fs->regs.reg[4].how = REG_SAVED_OFFSET;
  fs->regs.reg[4].loc.offset = (long)&sc->rsi - new_cfa;
  fs->regs.reg[5].how = REG_SAVED_OFFSET;
  fs->regs.reg[5].loc.offset = (long)&sc->rdi - new_cfa;
  fs->regs.reg[6].how = REG_SAVED_OFFSET;
  fs->regs.reg[6].loc.offset = (long)&sc->rbp - new_cfa;
  fs->regs.reg[8].how = REG_SAVED_OFFSET;
  fs->regs.reg[8].loc.offset = (long)&sc->r8 - new_cfa;
  fs->regs.reg[9].how = REG_SAVED_OFFSET;
  fs->regs.reg[9].loc.offset = (long)&sc->r9 - new_cfa;
  fs->regs.reg[10].how = REG_SAVED_OFFSET;
  fs->regs.reg[10].loc.offset = (long)&sc->r10 - new_cfa;
  fs->regs.reg[11].how = REG_SAVED_OFFSET;
  fs->regs.reg[11].loc.offset = (long)&sc->r11 - new_cfa;
  fs->regs.reg[12].how = REG_SAVED_OFFSET;
  fs->regs.reg[12].loc.offset = (long)&sc->r12 - new_cfa;
  fs->regs.reg[13].how = REG_SAVED_OFFSET;
  fs->regs.reg[13].loc.offset = (long)&sc->r13 - new_cfa;
  fs->regs.reg[14].how = REG_SAVED_OFFSET;
  fs->regs.reg[14].loc.offset = (long)&sc->r14 - new_cfa;
  fs->regs.reg[15].how = REG_SAVED_OFFSET;
  fs->regs.reg[15].loc.offset = (long)&sc->r15 - new_cfa;
  fs->regs.reg[16].how = REG_SAVED_OFFSET;
  fs->regs.reg[16].loc.offset = (long)&sc->rip - new_cfa;
  fs->retaddr_column = 16;
  fs->signal_frame = 1;
  return _URC_NO_REASON;
}

#else /* ifdef __x86_64__  */

#define MD_FALLBACK_FRAME_STATE_FOR x86_fallback_frame_state

static _Unwind_Reason_Code
x86_fallback_frame_state (struct _Unwind_Context *context,
			  _Unwind_FrameState *fs)
{
  unsigned char *pc = context->ra;
  struct sigcontext *sc;
  long new_cfa;

  /* popl %eax ; movl $__NR_sigreturn,%eax ; int $0x80  */
  if (*(unsigned short *)(pc+0) == 0xb858
      && *(unsigned int *)(pc+2) == 119
      && *(unsigned short *)(pc+6) == 0x80cd)
    sc = context->cfa + 4;
  /* movl $__NR_rt_sigreturn,%eax ; int $0x80  */
  else if (*(unsigned char *)(pc+0) == 0xb8
	   && *(unsigned int *)(pc+1) == 173
	   && *(unsigned short *)(pc+5) == 0x80cd)
    {
      struct rt_sigframe {
	int sig;
	siginfo_t *pinfo;
	void *puc;
	siginfo_t info;
	// struct ucontext uc;
 	ucontext_t uc;
      } *rt_ = context->cfa;
      /* The void * cast is necessary to avoid an aliasing warning.
         The aliasing warning is correct, but should not be a problem
         because it does not alias anything.  */
      sc = (struct sigcontext *) (void *) &rt_->uc.uc_mcontext;
    }
  else
    return _URC_END_OF_STACK;

  new_cfa = sc->esp;
  fs->regs.cfa_how = CFA_REG_OFFSET;
  fs->regs.cfa_reg = 4;
  fs->regs.cfa_offset = new_cfa - (long) context->cfa;

  /* The SVR4 register numbering macros aren't usable in libgcc.  */
  fs->regs.reg[0].how = REG_SAVED_OFFSET;
  fs->regs.reg[0].loc.offset = (long)&sc->eax - new_cfa;
  fs->regs.reg[3].how = REG_SAVED_OFFSET;
  fs->regs.reg[3].loc.offset = (long)&sc->ebx - new_cfa;
  fs->regs.reg[1].how = REG_SAVED_OFFSET;
  fs->regs.reg[1].loc.offset = (long)&sc->ecx - new_cfa;
  fs->regs.reg[2].how = REG_SAVED_OFFSET;
  fs->regs.reg[2].loc.offset = (long)&sc->edx - new_cfa;
  fs->regs.reg[6].how = REG_SAVED_OFFSET;
  fs->regs.reg[6].loc.offset = (long)&sc->esi - new_cfa;
  fs->regs.reg[7].how = REG_SAVED_OFFSET;
  fs->regs.reg[7].loc.offset = (long)&sc->edi - new_cfa;
  fs->regs.reg[5].how = REG_SAVED_OFFSET;
  fs->regs.reg[5].loc.offset = (long)&sc->ebp - new_cfa;
  fs->regs.reg[8].how = REG_SAVED_OFFSET;
  fs->regs.reg[8].loc.offset = (long)&sc->eip - new_cfa;
  fs->retaddr_column = 8;
  fs->signal_frame = 1;
  return _URC_NO_REASON;
}

#define MD_FROB_UPDATE_CONTEXT x86_frob_update_context

/* Fix up for kernels that have vDSO, but don't have S flag in it.  */

static void
x86_frob_update_context (struct _Unwind_Context *context,
			 _Unwind_FrameState *fs ATTRIBUTE_UNUSED)
{
  unsigned char *pc = context->ra;

  /* movl $__NR_rt_sigreturn,%eax ; {int $0x80 | syscall}  */
  if (*(unsigned char *)(pc+0) == 0xb8
      && *(unsigned int *)(pc+1) == 173
      && (*(unsigned short *)(pc+5) == 0x80cd
	  || *(unsigned short *)(pc+5) == 0x050f))
    _Unwind_SetSignalFrame (context, 1);
}

#endif /* ifdef __x86_64__  */
#endif /* not glibc 2.0 */
#endif /* ifdef inhibit_libc  */

重新编译即可。

原理:glibc2.25+移除了struct ucontext 取而代之的是一个新类型:ucontext_t,目前来看该类型与以前的结构相兼容,可以通过修改源文件的方法同滚编译。编译后的二进制经过测试,看起来没有问题。

后注:次解决方案丑陋无比,除非万不得已,还是打patch的好!!!

猜你喜欢

转载自blog.csdn.net/wang805447391/article/details/83380302