Use shared object files for ELF
authorJohann <johannkoenig@google.com>
Tue, 31 May 2011 15:59:04 +0000 (11:59 -0400)
committerJohann <johannkoenig@google.com>
Mon, 6 Jun 2011 20:10:31 +0000 (16:10 -0400)
Fixes #326

Change-Id: I5f2a4257430ef62f674190acefd43a0474821288

build/make/Makefile
build/make/obj_int_extract.c
libs.mk

index a951e99..011177c 100755 (executable)
@@ -220,11 +220,14 @@ define so_template
 # for creating them.
 #
 # This needs further abstraction for dealing with non-GNU linkers.
+comma := ,
 $(1):
        $(if $(quiet),@echo "    [LD] $$@")
        $(qexec)$$(LD) -shared $$(LDFLAGS) \
-            -Wl,--no-undefined -Wl,-soname,$$(SONAME) \
-            -Wl,--version-script,$$(SO_VERSION_SCRIPT) -o $$@ \
+            -Wl,--no-undefined \
+            $$(if $$(SONAME), -Wl$$(comma)-soname$$(comma)$$(SONAME)) \
+            $$(if $$(SO_VERSION_SCRIPT), -Wl$$(comma)--version-script$$(comma)$$(SO_VERSION_SCRIPT)) \
+            -o $$@ \
             $$(filter %.o,$$?) $$(extralibs)
 endef
 
@@ -291,7 +294,7 @@ LIBS=$(call enabled,LIBS)
 .libs: $(LIBS)
        @touch $@
 $(foreach lib,$(filter %_g.a,$(LIBS)),$(eval $(call archive_template,$(lib))))
-$(foreach lib,$(filter %so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH),$(LIBS)),$(eval $(call so_template,$(lib))))
+$(foreach lib,$(filter %.so %so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH),$(LIBS)),$(eval $(call so_template,$(lib))))
 
 INSTALL-LIBS=$(call cond_enabled,CONFIG_INSTALL_LIBS,INSTALL-LIBS)
 ifeq ($(MAKECMDGOALS),dist)
index 04e14a6..2f212f7 100644 (file)
@@ -347,6 +347,46 @@ bail:
     return 1;
 }
 
+int parse_elf_program(elf_obj_t *elf, int idx, Elf32_Phdr *phdr32, Elf64_Phdr *phdr64)
+{
+    if (phdr32)
+    {
+        if (idx >= elf->hdr32.e_phnum)
+            goto bail;
+
+        COPY_STRUCT(phdr32, elf->buf, elf->hdr32.e_phoff + idx * elf->hdr32.e_phentsize,
+                    elf->sz);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_type);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_offset);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_vaddr);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_paddr);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_filesz);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_memsz);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_flags);
+        ENDIAN_ASSIGN_IN_PLACE(phdr32->p_align);
+    }
+    else /* if (phdr64) */
+    {
+        if (idx >= elf->hdr64.e_phnum)
+            goto bail;
+
+        COPY_STRUCT(phdr64, elf->buf, elf->hdr64.e_phoff + idx * elf->hdr64.e_phentsize,
+                    elf->sz);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_type);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_offset);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_vaddr);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_paddr);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_filesz);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_memsz);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_flags);
+        ENDIAN_ASSIGN_IN_PLACE(phdr64->p_align);
+    }
+
+    return 0;
+bail:
+    return 1;
+}
+
 char *parse_elf_string_table(elf_obj_t *elf, int s_idx, int idx)
 {
     if (elf->bits == 32)
@@ -411,15 +451,19 @@ int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
     int          i;
     Elf32_Off    strtab_off32;
     Elf64_Off    strtab_off64; /* save String Table offset for later use */
+    Elf32_Word   load_alignment32;
+    Elf64_Xword  load_alignment64;
 
     memset(&elf, 0, sizeof(elf));
     elf.buf = buf;
     elf.sz = sz;
 
-    /* Parse Header */
     if (parse_elf_header(&elf))
       goto bail;
 
+    /* Parse Section and Program headers for string table offset and data
+     * aligment, respectively.
+     */
     if (elf.bits == 32)
     {
         Elf32_Shdr shdr;
@@ -441,6 +485,15 @@ int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
                 }
             }
         }
+        Elf32_Phdr phdr;
+        for (i = 0; i< elf.hdr32.e_phnum; i++)
+        {
+            parse_elf_program(&elf, i, &phdr, NULL);
+            if (phdr.p_type == PT_LOAD)
+            {
+                load_alignment32 = phdr.p_align;
+            }
+        }
     }
     else /* if (elf.bits == 64) */
     {
@@ -463,6 +516,15 @@ int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
                 }
             }
         }
+        Elf64_Phdr phdr;
+        for (i = 0; i< elf.hdr64.e_phnum; i++)
+        {
+            parse_elf_program(&elf, i, NULL, &phdr);
+            if (phdr.p_type == PT_LOAD)
+            {
+                load_alignment64 = phdr.p_align;
+            }
+        }
     }
 
     /* Parse all Symbol Tables */
@@ -473,7 +535,7 @@ int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
         {
             parse_elf_section(&elf, i, &shdr, NULL);
 
-            if (shdr.sh_type == SHT_SYMTAB)
+            if (shdr.sh_type == SHT_DYNSYM)
             {
                 for (ofst = shdr.sh_offset;
                      ofst < shdr.sh_offset + shdr.sh_size;
@@ -520,7 +582,7 @@ int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
                             }
 
                             memcpy(&val,
-                                   elf.buf + dhdr.sh_offset + sym.st_value,
+                                   elf.buf + (sym.st_value - load_alignment32),
                                    sym.st_size);
                         }
 
@@ -565,7 +627,7 @@ int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
         {
             parse_elf_section(&elf, i, NULL, &shdr);
 
-            if (shdr.sh_type == SHT_SYMTAB)
+            if (shdr.sh_type == SHT_DYNSYM)
             {
                 for (ofst = shdr.sh_offset;
                      ofst < shdr.sh_offset + shdr.sh_size;
@@ -612,7 +674,7 @@ int parse_elf(uint8_t *buf, size_t sz, output_fmt_t mode)
                             }
 
                             memcpy(&val,
-                                   elf.buf + dhdr.sh_offset + sym.st_value,
+                                   elf.buf + (sym.st_value - load_alignment64),
                                    sym.st_size);
                         }
 
diff --git a/libs.mk b/libs.mk
index 2cb7f49..2469cdf 100644 (file)
--- a/libs.mk
+++ b/libs.mk
@@ -257,12 +257,27 @@ $(filter %$(ASM).o,$(OBJS-yes)): $(BUILD_PFX)vpx_config.asm
 #
 # Calculate platform- and compiler-specific offsets for hand coded assembly
 #
+
+#
+# Parse shared object files for elf, object files for mach-o and coff
+#
+ifeq ($(TGT_OS), linux)
+OIE_LIB:=yes
+OIE_EXT:=.so
+else
+OIE_EXT:=.o
+endif
+
 ifeq ($(CONFIG_EXTERNAL_BUILD),) # Visual Studio uses obj_int_extract.bat
   ifeq ($(ARCH_ARM), yes)
     asm_com_offsets.asm: obj_int_extract
-    asm_com_offsets.asm: $(VP8_PREFIX)common/asm_com_offsets.c.o
+    asm_com_offsets.asm: $(VP8_PREFIX)common/asm_com_offsets.c$(OIE_EXT)
        ./obj_int_extract rvds $< $(ADS2GAS) > $@
     OBJS-yes += $(VP8_PREFIX)common/asm_com_offsets.c.o
+    ifeq ($(OIE_LIB), yes)
+      $(VP8_PREFIX)common/asm_com_offsets.c.so: $(VP8_PREFIX)common/asm_com_offsets.c.o
+      LIBS-yes += $(VP8_PREFIX)common/asm_com_offsets.c.so
+    endif
     CLEAN-OBJS += asm_com_offsets.asm
     $(filter %$(ASM).o,$(OBJS-yes)): $(BUILD_PFX)asm_com_offsets.asm
   endif
@@ -270,9 +285,13 @@ ifeq ($(CONFIG_EXTERNAL_BUILD),) # Visual Studio uses obj_int_extract.bat
   ifeq ($(ARCH_ARM)$(ARCH_X86)$(ARCH_X86_64), yes)
     ifeq ($(CONFIG_VP8_ENCODER), yes)
       asm_enc_offsets.asm: obj_int_extract
-      asm_enc_offsets.asm: $(VP8_PREFIX)encoder/asm_enc_offsets.c.o
+      asm_enc_offsets.asm: $(VP8_PREFIX)encoder/asm_enc_offsets.c$(OIE_EXT)
        ./obj_int_extract rvds $< $(ADS2GAS) > $@
       OBJS-yes += $(VP8_PREFIX)encoder/asm_enc_offsets.c.o
+      ifeq ($(OIE_LIB), yes)
+        $(VP8_PREFIX)encoder/asm_enc_offsets.c.so: $(VP8_PREFIX)encoder/asm_enc_offsets.c.o
+        LIBS-yes += $(VP8_PREFIX)encoder/asm_enc_offsets.c.so
+      endif
       CLEAN-OBJS += asm_enc_offsets.asm
       $(filter %$(ASM).o,$(OBJS-yes)): $(BUILD_PFX)asm_enc_offsets.asm
     endif
@@ -281,9 +300,13 @@ ifeq ($(CONFIG_EXTERNAL_BUILD),) # Visual Studio uses obj_int_extract.bat
   ifeq ($(ARCH_ARM), yes)
     ifeq ($(CONFIG_VP8_DECODER), yes)
       asm_dec_offsets.asm: obj_int_extract
-      asm_dec_offsets.asm: $(VP8_PREFIX)decoder/asm_dec_offsets.c.o
+      asm_dec_offsets.asm: $(VP8_PREFIX)decoder/asm_dec_offsets.c$(OIE_EXT)
        ./obj_int_extract rvds $< $(ADS2GAS) > $@
       OBJS-yes += $(VP8_PREFIX)decoder/asm_dec_offsets.c.o
+      ifeq ($(OIE_LIB), yes)
+        $(VP8_PREFIX)decoder/asm_dec_offsets.c.so: $(VP8_PREFIX)decoder/asm_dec_offsets.c.o
+        LIBS-yes += $(VP8_PREFIX)decoder/asm_dec_offsets.c.so
+      endif
       CLEAN-OBJS += asm_dec_offsets.asm
       $(filter %$(ASM).o,$(OBJS-yes)): $(BUILD_PFX)asm_dec_offsets.asm
     endif