@@ -673,13 +673,48 @@ struct elf *elf_open_read(const char *name, int flags)
673
673
return NULL ;
674
674
}
675
675
676
+ static int elf_add_string (struct elf * elf , struct section * strtab , char * str )
677
+ {
678
+ Elf_Data * data ;
679
+ Elf_Scn * s ;
680
+ int len ;
681
+
682
+ if (!strtab )
683
+ strtab = find_section_by_name (elf , ".strtab" );
684
+ if (!strtab ) {
685
+ WARN ("can't find .strtab section" );
686
+ return -1 ;
687
+ }
688
+
689
+ s = elf_getscn (elf -> elf , strtab -> idx );
690
+ if (!s ) {
691
+ WARN_ELF ("elf_getscn" );
692
+ return -1 ;
693
+ }
694
+
695
+ data = elf_newdata (s );
696
+ if (!data ) {
697
+ WARN_ELF ("elf_newdata" );
698
+ return -1 ;
699
+ }
700
+
701
+ data -> d_buf = str ;
702
+ data -> d_size = strlen (str ) + 1 ;
703
+ data -> d_align = 1 ;
704
+
705
+ len = strtab -> len ;
706
+ strtab -> len += data -> d_size ;
707
+ strtab -> changed = true;
708
+
709
+ return len ;
710
+ }
711
+
676
712
struct section * elf_create_section (struct elf * elf , const char * name ,
677
713
unsigned int sh_flags , size_t entsize , int nr )
678
714
{
679
715
struct section * sec , * shstrtab ;
680
716
size_t size = entsize * nr ;
681
717
Elf_Scn * s ;
682
- Elf_Data * data ;
683
718
684
719
sec = malloc (sizeof (* sec ));
685
720
if (!sec ) {
@@ -736,7 +771,6 @@ struct section *elf_create_section(struct elf *elf, const char *name,
736
771
sec -> sh .sh_addralign = 1 ;
737
772
sec -> sh .sh_flags = SHF_ALLOC | sh_flags ;
738
773
739
-
740
774
/* Add section name to .shstrtab (or .strtab for Clang) */
741
775
shstrtab = find_section_by_name (elf , ".shstrtab" );
742
776
if (!shstrtab )
@@ -745,27 +779,9 @@ struct section *elf_create_section(struct elf *elf, const char *name,
745
779
WARN ("can't find .shstrtab or .strtab section" );
746
780
return NULL ;
747
781
}
748
-
749
- s = elf_getscn (elf -> elf , shstrtab -> idx );
750
- if (!s ) {
751
- WARN_ELF ("elf_getscn" );
782
+ sec -> sh .sh_name = elf_add_string (elf , shstrtab , sec -> name );
783
+ if (sec -> sh .sh_name == -1 )
752
784
return NULL ;
753
- }
754
-
755
- data = elf_newdata (s );
756
- if (!data ) {
757
- WARN_ELF ("elf_newdata" );
758
- return NULL ;
759
- }
760
-
761
- data -> d_buf = sec -> name ;
762
- data -> d_size = strlen (name ) + 1 ;
763
- data -> d_align = 1 ;
764
-
765
- sec -> sh .sh_name = shstrtab -> len ;
766
-
767
- shstrtab -> len += strlen (name ) + 1 ;
768
- shstrtab -> changed = true;
769
785
770
786
list_add_tail (& sec -> list , & elf -> sections );
771
787
elf_hash_add (elf -> section_hash , & sec -> hash , sec -> idx );
0 commit comments