Editing or Adding to ELF string tables


 
Thread Tools Search this Thread
Top Forums Programming Editing or Adding to ELF string tables
# 8  
Old 11-07-2008
I gave it a whirrl. While it gave me a nice tree-view of the elf binary, it was not so simple to add or change section data. It appears you have to do it "by hand" -- ie, create a table, calculate the correct pointer, and change the entry in the table for that pointer. Not particularly time-saving. Smilie
# 9  
Old 11-07-2008
Lightbulb Using ld to modify an executable

You can use the GNU linker (ld) to create a new executable that's identical as the old one, but additionally contains the section from a new file. Well, in theory. So far I haven't gotten it to work as hoped. Attached are the files you can use for playing around.

First, you take a C source file that contains only static strings:
Code:
static char* __version = "sample 1.0";
static char* __author = "otheus@gmail.com";

Second, you compile it as usual with a C compiler to an object file.
Code:
gcc -o version_tmp.o -c version.c

Third, create a ld script file that will be used by ldto create an object file containing the static strings in your source, but in a ".comment" section.
Code:
SECTIONS
{
  .comment       0 : { *(.rodata) }
  /DISCARD/        : { *(.text .bss .data .comment .note.GNU-stack) }
}

Fourth, use the linker with this special ld script file ("commentsonly.lds") on your object file (created above with the gcc command). Now, instead of the strings being in the ".rodata" section, they are stored in the ".comment" section. Use the -r option so an object file is created.
Code:
ld -r -T commentsonly.lds -o version.o version_tmp.o

Finally, relink the original program with the new object file:
Code:
ld -o new_executable original_executable version.o

Now you can confirm the data is stored in this file using readelf or objdump:
Code:
objdump -s -j .comment new_executable

Here you should see the data you stored in your .c program.

So what happens. When I tried it, I got a segmentation fault. I suspect you have to do some address juggling to get this to work right. Let me know if you get anywhere?
# 10  
Old 11-07-2008
If you want to add a new section to an object file from an outside object file can you not do
Code:
objdump --target=<your triplet> --add-section SECTIONNAME==version.o \ 
   old_exe new_exe

objdump will identify the triplet of BFDNAME for the original for you ahead of time. This approoach will only work if .comment is a valid sectionname for your format. It should be a readonly section.

I cannot try this -- I'm on HPUX for the near future.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Editing in vi - adding words to a line

i have lines in a file similar to this: results=$(echo Total: ${res} | command | command) now I need to add text before the word "results", but i dont know how to do it. here's what i tried: %s~results=.*echo Total:.* ${7}~PROCESS \; results=.*echo Total:.* ${7}~g the problem is,... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

Need help regarding String editing

Hi Geeks I am working on trimming the logs and extracting the XMLs from it. I am facing one problem here. My XML String is ending with ...........Request></Body></Envelope>S/R sometimes there is more then just S/R in the end. I want to delete anything comes after </Envelope>... (3 Replies)
Discussion started by: santy00110011
3 Replies

3. Shell Programming and Scripting

Editing part of the string

Hi guys got a problem here hope u all can help me. I learn that sed can actually edit a string but you need to know the old attribute to change to new 1. Example: sed "s/$title:$author/$title:$Nauthor/g" "Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50" Each delimiter : represent... (4 Replies)
Discussion started by: GQiang
4 Replies

4. UNIX for Dummies Questions & Answers

Help with editing string elements

Hi All I have a question. I would like to edit some string characters by replacing with characters of choice located in another file. For example in sample file>S5_SK1.chr01 NNNNNNNNNNNNNNNNNNNCAGCATGCAATAAGGTGACATAGATATACCCACACACCACACCCTAACACTAACCCTAATCTAACCCTGGCCAACCTGTTT... (9 Replies)
Discussion started by: pawannoel
9 Replies

5. Programming

ELF-string table

hello everybody! I want to read the string table of an object file(which is in ELF format). I get the sh_name value but i cant find a way to read the value in the string table that this index represent. I program in C. thanx a lot folks! (3 Replies)
Discussion started by: nicos
3 Replies

6. Shell Programming and Scripting

SH Script help. editing string

I have a string that looks like this username|field1|field2|field3 the data has a delimiter of "|" how can i edit field1, keeping the rest of the data the same also how can i edit field2 and 3. (3 Replies)
Discussion started by: nookie
3 Replies

7. Shell Programming and Scripting

string editing in files

Hi all, I'm fairly new to scripting in linux and need some help. I have an file that looks something like this: ~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Some comments # Some comments # Some comments # Some comments # Some comments # Some comments abc:/path/to/somewhere:X... (3 Replies)
Discussion started by: Avatar Gixxer
3 Replies

8. Linux

editing ELF file

Hello, This is not exactly relevant to Linux kernel but I'm gonna ask any way. Is there any way I can modify a 64-bit ELF object file to make it look like 32-bit ELF object file and link it (using `ld`) with 32-bit ELF file? I tried libelf but was unsuccessful. I had this pretty link... (1 Reply)
Discussion started by: tejuwala
1 Replies
Login or Register to Ask a Question