@@ -717,11 +717,11 @@ static int elf_update_symbol(struct elf *elf, struct section *symtab,
717717}
718718
719719static struct symbol *
720- elf_create_section_symbol (struct elf * elf , struct section * sec )
720+ __elf_create_symbol (struct elf * elf , struct symbol * sym )
721721{
722722 struct section * symtab , * symtab_shndx ;
723723 Elf32_Word first_non_local , new_idx ;
724- struct symbol * sym , * old ;
724+ struct symbol * old ;
725725
726726 symtab = find_section_by_name (elf , ".symtab" );
727727 if (symtab ) {
@@ -731,27 +731,16 @@ elf_create_section_symbol(struct elf *elf, struct section *sec)
731731 return NULL ;
732732 }
733733
734- sym = calloc (1 , sizeof (* sym ));
735- if (!sym ) {
736- perror ("malloc" );
737- return NULL ;
738- }
739-
740- sym -> name = sec -> name ;
741- sym -> sec = sec ;
734+ new_idx = symtab -> sh .sh_size / symtab -> sh .sh_entsize ;
742735
743- // st_name 0
744- sym -> sym .st_info = GELF_ST_INFO (STB_LOCAL , STT_SECTION );
745- // st_other 0
746- // st_value 0
747- // st_size 0
736+ if (GELF_ST_BIND (sym -> sym .st_info ) != STB_LOCAL )
737+ goto non_local ;
748738
749739 /*
750740 * Move the first global symbol, as per sh_info, into a new, higher
751741 * symbol index. This fees up a spot for a new local symbol.
752742 */
753743 first_non_local = symtab -> sh .sh_info ;
754- new_idx = symtab -> sh .sh_size / symtab -> sh .sh_entsize ;
755744 old = find_symbol_by_index (elf , first_non_local );
756745 if (old ) {
757746 old -> idx = new_idx ;
@@ -769,18 +758,43 @@ elf_create_section_symbol(struct elf *elf, struct section *sec)
769758 new_idx = first_non_local ;
770759 }
771760
761+ /*
762+ * Either way, we will add a LOCAL symbol.
763+ */
764+ symtab -> sh .sh_info += 1 ;
765+
766+ non_local :
772767 sym -> idx = new_idx ;
773768 if (elf_update_symbol (elf , symtab , symtab_shndx , sym )) {
774769 WARN ("elf_update_symbol" );
775770 return NULL ;
776771 }
777772
778- /*
779- * Either way, we added a LOCAL symbol.
780- */
781- symtab -> sh .sh_info += 1 ;
773+ return sym ;
774+ }
775+
776+ static struct symbol *
777+ elf_create_section_symbol (struct elf * elf , struct section * sec )
778+ {
779+ struct symbol * sym = calloc (1 , sizeof (* sym ));
780+
781+ if (!sym ) {
782+ perror ("malloc" );
783+ return NULL ;
784+ }
782785
783- elf_add_symbol (elf , sym );
786+ sym -> name = sec -> name ;
787+ sym -> sec = sec ;
788+
789+ // st_name 0
790+ sym -> sym .st_info = GELF_ST_INFO (STB_LOCAL , STT_SECTION );
791+ // st_other 0
792+ // st_value 0
793+ // st_size 0
794+
795+ sym = __elf_create_symbol (elf , sym );
796+ if (sym )
797+ elf_add_symbol (elf , sym );
784798
785799 return sym ;
786800}
0 commit comments