last day (15 days later) » 

7:46 PM
1
A: How to get rid of DLL .reloc section using MinGW-gcc?

Kuba OberPIC doesn't mean position-independent data. Your code is position independent, but that incurs run-time costs. There's no magic by which the data section can be populated with function addresses at compile/link time, since they vary at runtime - otherwise PIE wouldn't have said runtime costs to ...

 
What is the (minimalist) way to go? Build vtables at runtime (like CPython does) or rely on the loader for relocation (like C++ does, I think)?
Besides then, where exactly is the difference of -fpic and -pie on GCC?
 
@PhilipDahnen A vtable is just one way of implementing dynamic dispatch. At the end of the day, you know exactly what functions to call, so you can implement a thunk yourself. Why do you hate relocations so much?
@PhilipDahnen For pic vs pie see this question. TL;DR: PIE is like PIC, but for executables and comes at a slightly lower cost in some cases.
 
I don't hate them. I just want to know if there is a smarter way, as they incur runtime cost (as you stated). Your solution is good, but has the consequence that all functions need to be imported.
 
@PhilipDahnen That's not a big deal, the signatures must match so you can macro-ize it out.
 
As I said, your solution is good, but I think there can be a more concise solution. What about setting --base-address=X and then saying &C_foo_impl + X in the vtable, would this work?
 
7:54 PM
Is that C or pseudocode? I have no idea what X is in C, and how you think of adding anything to a function pointer. Arithmetic on function pointers is a gcc extension and they are treated as pointers to a type with size 1. I'd consider it an ugly and unnecessary hack. The costs of runtime relocation by the linker are negligible unless you have a proof otherwise. You're wasting your time, as far as I can tell...
Unless this is for an embedded-style application where all the code is in read-only fixed-position storage. In such case you don't want PIC/PIE to start with.
Or, think of it this way: if your project is the size of OpenOffice or Firefox, then you might need to worry. Otherwise, you'll be hard-pressed to even measure the overhead of these vtable relocations you have.
 
I mean that I compile with, e. g. --base-address=0x20000 and then in the vtable hard-encode this offset.
I'm actually designing an object-oriented language for my thesis.
 
Then don't use PIC/PIE to start with. They don't make any sense if you have fixed-position code.
So, you cross-compile from your language to C?
 
Correct. I wrote some sort of preprocessor.
 
Then why do you use PIC/PIE if you clearly intend for the resulting binary to be only loaded at a fixed position? Makes 0 sense.
 
How do I keep GCC from making relocations?
making=inserting into binary
After all I didn't "intend", this is just a way of thought.
 
8:02 PM
If you want the DLL to have a fixed base address, you must instruct the linker to link the DLL as such. I presume you use mingw linker, then you need to pass --image-base and --disable-auto-image-base, and you need to inspect the platform specs file for possible default --enable-auto-image-base (and remove it).
 
OK, I will try that. Maybe --disable-auto-image-base is what I was looking for.
 
Sorry, no need to change the specs file. Just give -Wl,--image-base=x -Wl,--disable-auto-image-base to gcc.
 
Btw, is the a macro to access the image-base from within code?
*there
I didn't find it on GCC site...
 
No need for one. You pass the arguments to the compiler. You can define whatever you wish. In any case, once you generate fixed position code, you don't care anymore where your code resides: the base is only useful to the linker.
I mean, your preprocessor should, at the end, compile just like gcc does. That you use gcc is an implementation detail, after all. You can specify bases for every library you generate as you wish.
 
You mean I have to do it myself: #define IMAGE_BASE 0x20000
 
8:09 PM
Yes. But you won't have a need to know this from within the C code anyway. Or at least I don't see the need: the binary is fixed-position code, the vtables use fixed addresses, and so on.
 
And then say -Wl,--image-base=0x20000,--disable-auto-image-base
I don't need to say &function + BASE_ADDRESS in the vtable?!
 
I think you're really overthinking it by micro-optimizing things. Unless you can show that the DLL linker takes significant time in doing runtime relocations, you shouldn't worry. let the linker do its job, and focus on the language design.
No, you don't need to do any such things. When the compiler/linker emit a fixed-position DLL, all the addresses are static and fully resolved.
That's the whole point of having a fixed image address.
Besides, C code like &function + BASE_ADDRESS is almost always nonsense. You'll not need to write it, because the compiler won't ever emit code that could make it useful.
 
Oh man, you just enlightened me!!! THANX SO MUCH!!!
I got it now, OK, have a nice day!
 
Basically, (&fun + integer) will almost always crash, unless the integer is zero.
 
Yeah, that makes sence.
 
8:15 PM
Think of it like this: you can use your vtable approach, and have fixed base addresses and position-dependent code and then your DLL won't have relocations for the vtables. It will have relocations for other things, since you will call likely other DLLs that are relocatable. And if you use position-independent code, your vtables will have relocations: that's the runtime cost of position-independence!
Relocating a vtable by the PE runtime linker/loader is much faster than you populating the vtable from your own code at runtime. The PE loader is optimized to heck and back.
 
But it works with fixed-position DLLs! Could you add a minimal answer that says that and I will accept it.
 
Nah, just answer your own question, once you verify that my suggestions above work for you.
Have a good day.
 
You're my hero of the day.
If you are interested in getting an acknowlegdment in and a copy of my thesis, leave me your email.
 

last day (15 days later) »