@@ -715,6 +715,66 @@ static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
715
715
return len ;
716
716
}
717
717
718
+ struct symbol * elf_create_undef_symbol (struct elf * elf , const char * name )
719
+ {
720
+ struct section * symtab ;
721
+ struct symbol * sym ;
722
+ Elf_Data * data ;
723
+ Elf_Scn * s ;
724
+
725
+ sym = malloc (sizeof (* sym ));
726
+ if (!sym ) {
727
+ perror ("malloc" );
728
+ return NULL ;
729
+ }
730
+ memset (sym , 0 , sizeof (* sym ));
731
+
732
+ sym -> name = strdup (name );
733
+
734
+ sym -> sym .st_name = elf_add_string (elf , NULL , sym -> name );
735
+ if (sym -> sym .st_name == -1 )
736
+ return NULL ;
737
+
738
+ sym -> sym .st_info = GELF_ST_INFO (STB_GLOBAL , STT_NOTYPE );
739
+ // st_other 0
740
+ // st_shndx 0
741
+ // st_value 0
742
+ // st_size 0
743
+
744
+ symtab = find_section_by_name (elf , ".symtab" );
745
+ if (!symtab ) {
746
+ WARN ("can't find .symtab" );
747
+ return NULL ;
748
+ }
749
+
750
+ s = elf_getscn (elf -> elf , symtab -> idx );
751
+ if (!s ) {
752
+ WARN_ELF ("elf_getscn" );
753
+ return NULL ;
754
+ }
755
+
756
+ data = elf_newdata (s );
757
+ if (!data ) {
758
+ WARN_ELF ("elf_newdata" );
759
+ return NULL ;
760
+ }
761
+
762
+ data -> d_buf = & sym -> sym ;
763
+ data -> d_size = sizeof (sym -> sym );
764
+ data -> d_align = 1 ;
765
+
766
+ sym -> idx = symtab -> len / sizeof (sym -> sym );
767
+
768
+ symtab -> len += data -> d_size ;
769
+ symtab -> changed = true;
770
+
771
+ sym -> sec = find_section_by_index (elf , 0 );
772
+
773
+ elf_add_symbol (elf , sym );
774
+
775
+ return sym ;
776
+ }
777
+
718
778
struct section * elf_create_section (struct elf * elf , const char * name ,
719
779
unsigned int sh_flags , size_t entsize , int nr )
720
780
{
0 commit comments