+/* Do ELF relocations which lack an explicit addend. All x86-linux
+ relocations appear to be of this form. */
+static int
+do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
+ Elf32_Shdr* shdr, int shnum,
+ Elf32_Sym* stab, char* strtab )
+{
+ int j;
+ char *symbol;
+ Elf32_Word* targ;
+ Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
+ int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
+ int target_shndx = shdr[shnum].sh_info;
+ int symtab_shndx = shdr[shnum].sh_link;
+ stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
+ targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
+ IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
+ target_shndx, symtab_shndx ));
+ for (j = 0; j < nent; j++) {
+ Elf32_Addr offset = rtab[j].r_offset;
+ Elf32_Word info = rtab[j].r_info;
+
+ Elf32_Addr P = ((Elf32_Addr)targ) + offset;
+ Elf32_Word* pP = (Elf32_Word*)P;
+ Elf32_Addr A = *pP;
+ Elf32_Addr S;
+
+ IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
+ j, (void*)offset, (void*)info ));
+ if (!info) {
+ IF_DEBUG(linker,belch( " ZERO" ));
+ S = 0;
+ } else {
+ /* First see if it is a nameless local symbol. */
+ if (stab[ ELF32_R_SYM(info)].st_name == 0) {
+ symbol = "(noname)";
+ S = (Elf32_Addr)
+ (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
+ + stab[ELF32_R_SYM(info)].st_value);
+ } else {
+ /* No? Should be in a symbol table then; first try the
+ local one. */
+ symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
+ (void*)S = lookupLocalSymbol( oc, symbol );
+ if ((void*)S == NULL)
+ (void*)S = lookupSymbol( symbol );
+ }
+ if (!S) {
+ belch("%s: unknown symbol `%s'", oc->fileName, symbol);
+ return 0;
+ }
+ IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
+ }
+ IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
+ (void*)P, (void*)S, (void*)A ));
+ checkProddableBlock ( oc, pP );
+ switch (ELF32_R_TYPE(info)) {
+# ifdef i386_TARGET_ARCH
+ case R_386_32: *pP = S + A; break;
+ case R_386_PC32: *pP = S + A - P; break;
+# endif
+ default:
+ belch("%s: unhandled ELF relocation(Rel) type %d\n",
+ oc->fileName, ELF32_R_TYPE(info));
+ return 0;
+ }
+
+ }
+ return 1;
+}
+
+
+/* Do ELF relocations for which explicit addends are supplied.
+ sparc-solaris relocations appear to be of this form. */
+static int
+do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
+ Elf32_Shdr* shdr, int shnum,
+ Elf32_Sym* stab, char* strtab )
+{
+ int j;
+ char *symbol;
+ Elf32_Word* targ;
+ Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
+ int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
+ int target_shndx = shdr[shnum].sh_info;
+ int symtab_shndx = shdr[shnum].sh_link;
+ stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
+ targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
+ IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
+ target_shndx, symtab_shndx ));
+ for (j = 0; j < nent; j++) {
+ Elf32_Addr offset = rtab[j].r_offset;
+ Elf32_Word info = rtab[j].r_info;
+ Elf32_Addr P = ((Elf32_Addr)targ) + offset;
+ Elf32_Addr S;
+# if defined(sparc_TARGET_ARCH)
+ /* This #ifdef only serves to avoid unused-var warnings. */
+ Elf32_Sword addend = rtab[j].r_addend;
+ Elf32_Addr A = addend;
+ Elf32_Word* pP = (Elf32_Word*)P;
+ Elf32_Word w1, w2;
+# endif
+
+ IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
+ j, (void*)offset, (void*)info,
+ (void*)addend ));
+ if (!info) {
+ IF_DEBUG(linker,belch( " ZERO" ));
+ S = 0;
+ } else {
+ /* First see if it is a nameless local symbol. */
+ if (stab[ ELF32_R_SYM(info)].st_name == 0) {
+ symbol = "(noname)";
+ S = (Elf32_Addr)
+ (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
+ + stab[ELF32_R_SYM(info)].st_value);
+ } else {
+ /* No? Should be in a symbol table then; first try the
+ local one. */
+ symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
+ (void*)S = lookupLocalSymbol( oc, symbol );
+ if ((void*)S == NULL)
+ (void*)S = lookupSymbol( symbol );
+ }
+ if (!S) {
+ belch("%s: unknown symbol `%s'", oc->fileName, symbol);
+ return 0;
+ /*
+ S = 0x11223344;
+ fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
+ */
+ }
+ IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
+ }
+ IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
+ (void*)P, (void*)S, (void*)A ));
+ checkProddableBlock ( oc, (void*)P );
+ switch (ELF32_R_TYPE(info)) {
+# if defined(sparc_TARGET_ARCH)
+ case R_SPARC_WDISP30:
+ w1 = *pP & 0xC0000000;
+ w2 = (Elf32_Word)((S + A - P) >> 2);
+ ASSERT((w2 & 0xC0000000) == 0);
+ w1 |= w2;
+ *pP = w1;
+ break;
+ case R_SPARC_HI22:
+ w1 = *pP & 0xFFC00000;
+ w2 = (Elf32_Word)((S + A) >> 10);
+ ASSERT((w2 & 0xFFC00000) == 0);
+ w1 |= w2;
+ *pP = w1;
+ break;
+ case R_SPARC_LO10:
+ w1 = *pP & ~0x3FF;
+ w2 = (Elf32_Word)((S + A) & 0x3FF);
+ ASSERT((w2 & ~0x3FF) == 0);
+ w1 |= w2;
+ *pP = w1;
+ break;
+ /* According to the Sun documentation:
+ R_SPARC_UA32
+ This relocation type resembles R_SPARC_32, except it refers to an
+ unaligned word. That is, the word to be relocated must be treated
+ as four separate bytes with arbitrary alignment, not as a word
+ aligned according to the architecture requirements.
+
+ (JRS: which means that freeloading on the R_SPARC_32 case
+ is probably wrong, but hey ...)
+ */
+ case R_SPARC_UA32:
+ case R_SPARC_32:
+ w2 = (Elf32_Word)(S + A);
+ *pP = w2;
+ break;
+# endif
+ default:
+ belch("%s: unhandled ELF relocation(RelA) type %d\n",
+ oc->fileName, ELF32_R_TYPE(info));
+ return 0;
+ }
+
+ }
+ return 1;
+}
+
+