linker: Fix ARM_R_COPY relocations

Per http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
Section 4.7.1.10, ARM_R_COPY relocations are only suppose to reference shared
libraries, not the executable itself.  When resolving an R_ARM_COPY symbol,
ensure we don't look in our own symbol.

This partially addresses
http://code.google.com/p/android/issues/detail?id=28598 .  After this
patch, the printfs generated by the test program are:

global = 0x42 (0x401c7000)
global = 0x42 (0x11000)

before, the output was:

global = 0x42 (0x40071000)
global = 0x0 (0x11000)

I'm still not very happy with this patch, but I think it's an improvement
over where we were at before.

This change was modeled after https://android-review.googlesource.com/38871

Change-Id: Id7ad921e58395e76a36875bcc742ec5eeba53f08
This commit is contained in:
Nick Kralevich 2012-08-24 13:25:51 -07:00
parent a37ce7faa6
commit d39c3abd5a

View File

@ -434,13 +434,14 @@ static unsigned elfhash(const char *_name)
static Elf32_Sym * static Elf32_Sym *
soinfo_do_lookup(soinfo *si, const char *name, Elf32_Addr *offset, soinfo_do_lookup(soinfo *si, const char *name, Elf32_Addr *offset,
soinfo *needed[]) soinfo *needed[], bool ignore_local)
{ {
unsigned elf_hash = elfhash(name); unsigned elf_hash = elfhash(name);
Elf32_Sym *s; Elf32_Sym *s = NULL;
soinfo *lsi = si; soinfo *lsi = si;
int i; int i;
if (!ignore_local) {
/* Look for symbols in the local scope (the object who is /* Look for symbols in the local scope (the object who is
* searching). This happens with C++ templates on i386 for some * searching). This happens with C++ templates on i386 for some
* reason. * reason.
@ -454,6 +455,7 @@ soinfo_do_lookup(soinfo *si, const char *name, Elf32_Addr *offset,
s = soinfo_elf_lookup(si, elf_hash, name); s = soinfo_elf_lookup(si, elf_hash, name);
if(s != NULL) if(s != NULL)
goto done; goto done;
}
/* Next, look for it in the preloads list */ /* Next, look for it in the preloads list */
for(i = 0; preloads[i] != NULL; i++) { for(i = 0; preloads[i] != NULL; i++) {
@ -684,6 +686,7 @@ verify_elf_header(const Elf32_Ehdr* hdr)
if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1; if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1; if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1; if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
if (hdr->e_type != ET_DYN) return -1;
/* TODO: Should we verify anything else in the header? */ /* TODO: Should we verify anything else in the header? */
#ifdef ANDROID_ARM_LINKER #ifdef ANDROID_ARM_LINKER
@ -959,7 +962,11 @@ static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
} }
if(sym != 0) { if(sym != 0) {
sym_name = (char *)(strtab + symtab[sym].st_name); sym_name = (char *)(strtab + symtab[sym].st_name);
s = soinfo_do_lookup(si, sym_name, &offset, needed); bool ignore_local = false;
#if defined(ANDROID_ARM_LINKER)
ignore_local = (type == R_ARM_COPY);
#endif
s = soinfo_do_lookup(si, sym_name, &offset, needed, ignore_local);
if(s == NULL) { if(s == NULL) {
/* We only allow an undefined symbol if this is a weak /* We only allow an undefined symbol if this is a weak
reference.. */ reference.. */
@ -1139,10 +1146,29 @@ static int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
#ifdef ANDROID_ARM_LINKER #ifdef ANDROID_ARM_LINKER
case R_ARM_COPY: case R_ARM_COPY:
if ((si->flags & FLAG_EXE) == 0) {
/*
* http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
*
* Section 4.7.1.10 "Dynamic relocations"
* R_ARM_COPY may only appear in executable objects where e_type is
* set to ET_EXEC.
*
* TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
* We should explicitly disallow ET_DYN executables from having
* R_ARM_COPY relocations.
*/
DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
return -1;
}
count_relocation(kRelocCopy); count_relocation(kRelocCopy);
MARK(rel->r_offset); MARK(rel->r_offset);
TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid, TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
reloc, s->st_size, sym_addr, sym_name); reloc, s->st_size, sym_addr, sym_name);
if (reloc == sym_addr) {
DL_ERR("Internal linker error detected. reloc == symaddr");
return -1;
}
memcpy((void*)reloc, (void*)sym_addr, s->st_size); memcpy((void*)reloc, (void*)sym_addr, s->st_size);
break; break;
#endif /* ANDROID_ARM_LINKER */ #endif /* ANDROID_ARM_LINKER */
@ -1201,7 +1227,7 @@ static int mips_relocate_got(soinfo* si, soinfo* needed[]) {
/* This is an undefined reference... try to locate it */ /* This is an undefined reference... try to locate it */
sym_name = si->strtab + sym->st_name; sym_name = si->strtab + sym->st_name;
s = soinfo_do_lookup(si, sym_name, &base, needed); s = soinfo_do_lookup(si, sym_name, &base, needed, false);
if (s == NULL) { if (s == NULL) {
/* We only allow an undefined symbol if this is a weak /* We only allow an undefined symbol if this is a weak
reference.. */ reference.. */