Editing or Adding to ELF string tables


 
Thread Tools Search this Thread
Top Forums Programming Editing or Adding to ELF string tables
# 1  
Old 11-06-2008
Editing or Adding to ELF string tables

Is it possible to add - or edit - data (the strings) contained within the string table of an ELF executable?

I know I can access the string table with the following code;

while ((scn = elf_nextscn(m_elf, scn)) != 0) {
char *name = 0;
gelf_getshdr(scn, &shdr);
if (shdr.sh_type == SHT_STRTAB) {
if ( shdr.sh_size != 0 ) {
name = elf_strptr(m_elf, shstrndx, (size_t)shdr.sh_name);
std::cout << name << std::endl;
}
}
}

But can I edit - or add - particular fields to it?

I'm trying to add a field which will allow me to easily identify my executables with a particular attribute. Say Add a field in the string table called 'Version : xxxx'.

Thanks.
# 2  
Old 11-06-2008
add:
Code:
static char *revision_string="@(#) Revision: myfile.cpp 1.1.0 2008/10/03";

To your source code then use the what command to identify revisions
(see also: ident man page for a similar idea using "$... text here $")
Code:
what compile_file

will produce the string above.
# 3  
Old 11-06-2008
Doing a "man elf" I see that the ELF structure is as follows:
Code:
 #define EI_NIDENT 16

           typedef struct {
                   unsigned char  e_ident[EI_NIDENT];
                   uint16_t       e_type;
                   uint16_t       e_machine;
                   uint32_t       e_version;
                   ElfN_Addr      e_entry;
                   ElfN_Off       e_phoff;
                   ElfN_Off       e_shoff;
                   uint32_t       e_flags;
                   uint16_t       e_ehsize;
                   uint16_t       e_phentsize;
                   uint16_t       e_phnum;
                   uint16_t       e_shentsize;
                   uint16_t       e_shnum;
                   uint16_t       e_shstrndx;
           } ElfN_Ehdr;

Remarks:
Quote:
e_flags - This member holds processor-specific flags associated with the file. Flag names take the form EF_'machine_flag'. Currently no flags have been defined.
So you have 32 bits to fool around with until someone else defines a flag.

Last edited by otheus; 11-06-2008 at 10:51 AM.. Reason: formatting
# 4  
Old 11-06-2008
Perhaps I should clarify.

Assume that I don't have the source code - I can't compile extra info into the executable. I only have the binary.

There are various sections in the ELF headers - and after some research I thought that the best place to append strings would be the string tables. But, I'm not sure if it's possible to edit the binary - tho there is a tool called elfedit, written by a sun dev, which does exactly this (well, he modifies the runtime paths), but I can't find its source code. So - I'm assuming this behaviour is exposed in the API via libelf.

Some references -
http://people.freebsd.org/~jkoshy/download/libelf/article.html
Linker and Libraries Guide

Any ideas, guys?
# 5  
Old 11-06-2008
Quote:
e_flags - This member holds processor-specific flags associated with the file. Flag names take the form EF_'machine_flag'. Currently no flags have been defined.
This may be true for the x86 psABI but is not true for IA64, MIPS and more. For example the IA64 psABI specifies the following values for e_flags (Section 4.1.1.6 Table 4-2):
Quote:
EF_IA_64_MASKOS
EF_IA_64_ABI64
EF_IA_64_REDUCEDFP
EF_IA_64_CONS_GP
EF_IA_64_NOFUNCDESC_CONS_GP
EF_IA_64_ABSOLUTE
EF_IA_64_ARCH
# 6  
Old 11-07-2008
There's a program on Linux called "objcopy" but I couldn't get it to work. The GNU man page says this program can add sections.

Back to ELF layout: There's a "version" section named ".comment". Presumably, here's where you can put version strings. There's also a "notes" segment, a segment being something that contains sections. In here you can put anything you want.
Presumably, just rewriting the executable with the additional headers (containing segment and/or section references) will work.

I'm really curious about this, so let's continue to work together on this.
# 7  
Old 11-07-2008
Try this:
HT Editor the HT Editor.
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