How ObjDump retrieve labels from elf file

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

How ObjDump retrieve labels from elf file

Post by Newbie »

Hello everybody,

Looking around GCC, ELF and OBJDump, I ask myself a question.
When I insert a label in ASM code in a function (the main for example).

In C :

Code: Select all


	int main(int argc, char* argv[])
	{
		.....
		.....
		.....
		.....
		.....
		.....
		.....
		
		__asm__ __volatile__("START:");

		.....
		.....
		.....
		.....
		.....
		.....
		.....
		
		__asm__ __volatile__("END:");

		return 0;	
	}

After compiling it to obtain an ELF then I use OBJDump to examine the code.

In ELF :

8c010242 <START>:
8c010242: .....
8c010244: .....
8c010246: .....
8c010248: .....
8c01024a: .....
8c01024c: .....
8c01024e: .....
8c010250: .....
8c010252: .....
8c010254: .....
8c010256: .....
8c010258: .....
8c01025a <END>:
I found out the written labels in the C code.

But when I try to "Hexedit" the ELF file, searching for the labels ("START:", "START" ...), i did not found anything.

So how could OBJDump write at the correct place the desired label in the code as those labels seem to not exist in ELF file.

Thanks for your explanations.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: How ObjDump retrieve labels from elf file

Post by BlueCrab »

The labels are stored as debugging symbols in the elf file.
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Re: How ObjDump retrieve labels from elf file

Post by nymus »

Yes, the .elf file has a specific structure that the gcc tools know about so they can find the debug symbols easily. The labels are still there in text form, but since a "hexdump" program doesn't know the structure, it just reads the data and formats it in its own way e.g as bytes / shorts / longs etc.

A hexedit program "should" be able to find text, though. Which program are you using? Most hex editors have a data frame with raw data and a side frame that displays text. You can even see the symbol names if you execute the command: "grep --binary-files=text file.elf"

Attached is a screenshot of elvis (a vi clone) displaying a section of a .elf file. You can see the text on the far right corresponding to the raw data in the main window.

Note those labels are not structured as they appear in the assembly. They are all placed together in a special section and there are pointers to that text somewhere so that the tools know which text corresponds to which address in the code.
Attachments
Screen Shot 2016-07-11 at 6.36.27 PM.png
behold the mind
inspired by Dreamcast
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: How ObjDump retrieve labels from elf file

Post by Newbie »

Hi,

Thanks for answers, I use HXD (https://mh-nexus.de/en/hxd/).

Well, I expected a simpler way of reading labels to figure out position code in the elf file. Perhaps even writing some code to obtain it.

If it uses something like referencing an id used to retrieve the label string from a far located section in the elf file, I did not like that :(
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Re: How ObjDump retrieve labels from elf file

Post by nymus »

Try downloading dcload sources and see how t use libelf to decode elf files. It's maintained beside the KOS git repository.
behold the mind
inspired by Dreamcast
Post Reply