Register - Login
Views: 85582588
Main - Memberlist - Active users - Calendar - Wiki - IRC Chat - Online users
Ranks - Rules/FAQ - JCS - Stats - Latest Posts - Color Chart - Smilies
09-20-17 06:19:54 AM

Jul - Computers - Linking raw file with gcc? New poll - New thread - New reply
Next newer thread | Next older thread
shyguyhex

Level: 14


Posts: 30/45
EXP: 11113
For next: 1958

Since: 01-03-14


Since last post: 2.0 years
Last activity: 1.0 years

Posted on 09-21-14 12:21:02 PM (last edited by shyguyhex at 09-21-14 12:51:51 PM) Link | Quote
So I have two tables of structs that I would like to add into my project at link time.

I made the object files using objcopy:

objcopy -I binary -O elf32-i386 -B i386 asm_functions.bin objects\asm_functions.bin.o
objcopy -I binary -O elf32-i386 -B i386 asm_operations.bin objects\asm_operations.bin.o


Then I linked them with gcc:

gcc -w res\b64.res objects\asm_functions.bin.o objects\asm_operations.bin.o b64si.c -o b64si.exe


These are the symbols I'm suppose to use, but gcc tells me that they are undefined when I try to use them:

_binary_asm_operations_bin_start
_binary_asm_functions_bin_start


extern const int _binary_asm_operations_bin_start[];
extern const int _binary_asm_functions_bin_start[];


I know that these are the right symbols because I checked the object files with a hex editor; I checked the final exe as well, and my table and the symbols were in there. So I'm confused as to why the symbols aren't defined. Any help would be appreciated.

(win7 32bit)
shyguyhex

Level: 14


Posts: 31/45
EXP: 11113
For next: 1958

Since: 01-03-14


Since last post: 2.0 years
Last activity: 1.0 years

Posted on 09-21-14 04:17:58 PM Link | Quote
Managed to find a solution that works.


extern int testdata[] asm("_binary_asm_operations_bin_start");
andlabs
Member
Level: 34


Posts: 214/277
EXP: 241425
For next: 12226

Since: 03-18-10
From: United States

Since last post: 40 days
Last activity: 5 days

Posted on 09-21-14 04:26:39 PM Link | Quote
Late but what happens if you get rid of the leading underscore?
Joe
Common spammer
🗿
Level: 103


Posts: 3157/3286
EXP: 11461268
For next: 10138

Since: 08-02-07
From: Pororoca

Since last post: 45 days
Last activity: 18 hours

Posted on 09-21-14 07:08:41 PM Link | Quote
Originally posted by andlabs
leading underscore

This is the problem. The MinGW version of gcc adds an underscore to all symbols when it generates assembly code.

The correct fix is to remove the underscores in the C code.
shyguyhex

Level: 14


Posts: 32/45
EXP: 11113
For next: 1958

Since: 01-03-14


Since last post: 2.0 years
Last activity: 1.0 years

Posted on 09-21-14 10:13:57 PM (last edited by shyguyhex at 09-21-14 10:24:42 PM) Link | Quote
Thanks guys, that works. Now my code is less funky looking .

I find it rather confusing that all the examples I can find about binary linking have people using the leading underscores in their code though


http://balau82.wordpress.com/2012/02/19/linking-a-binary-blob-with-gcc/
http://bytbox.net/blog/2012/11/linking-raw-data.html
Joe
Common spammer
🗿
Level: 103


Posts: 3159/3286
EXP: 11461268
For next: 10138

Since: 08-02-07
From: Pororoca

Since last post: 45 days
Last activity: 18 hours

Posted on 09-22-14 08:45:26 AM Link | Quote
Those examples are all for the Linux (System V) ABI. The 32-bit Windows ABI has specific name mangling requirements, and MinGW follows them in order to maintain compatibility across different compilers and object files.

The 64-bit Windows ABI is much more similar to the System V ABI, so for something simple like this you could probably use the same code on both.

(Technically, there are multiple System V ABIs, one for each CPU architecture. As far as I know, they all have the same name mangling semantics, so I'm ignoring the differences here.)
Next newer thread | Next older thread
Jul - Computers - Linking raw file with gcc? New poll - New thread - New reply




Rusted Logic

Acmlmboard - commit 2f1bc75 [2017-08-27]
©2000-2017 Acmlm, Xkeeper, Inuyasha, et al.

27 database queries, 1 query cache hits.
Query execution time: 0.170321 seconds
Script execution time: 0.011406 seconds
Total render time: 0.181727 seconds