[challenge] Linker surgery

Rainer Schuetze r.sagitario at gmx.de
Sun May 19 00:02:48 PDT 2013



On 18.05.2013 09:46, Dmitry Olshansky wrote:
> 18-May-2013 10:40, Rainer Schuetze пишет:
>>
>>
>> If you compile all the files on the command line for the executable
>> everything gets dragged in.
>
> Then the only question would be - why we need this behavior?
> It looks painfully clear to me that -lib style should be the default.

I guess it is the compilation model inherited from C++. Putting 
everything into libraries has its own share of issues like not linking 
in modules that are only accessed via the object factory or that 
register with some other system in a static constructor.

>
>  >You will have to build the first two modules
>> into a library:
>>
>> dmd -lib fmod.d mod.d -ofm.lib
>> dmd -map driver.d m.lib
>> grep fmod driver.map
>>
>> Splitting functions into separate object files also only happens when
>> -lib is specified.
>
> Great then even this seems to work:
>
> module mod;
>
> int foo(int i)
> {
>      static immutable int[] fable = [1,2,3]; //table_.mangleof == "table_";
>      return fable[i];
> }
>
> public void car(){
>
> }
>
> //driver
> import mod;
>
> immutable byte[] bable = [1, 2, 3, 4, 5];
>
> byte boo(int ch){
>      return bable[ch];
> }
>
> void main(string[] args){
>      boo(0);
>      car();
> }
>

I just verified (for Win32) that it also works if you also defined data 
in the car function.

On 17.05.2013 19:57, Rainer Schuetze wrote:
 > Yes, if you build a library the functions in a module are split into
 > separate object files, but data is always written into the object
 > file of the original module. The linker cannot split these afterwards 
 > if any data in the module is referenced (which might happen by just
 > importing the module).

It seems I was wrong here. Function local data is written into the same
object file as the function. I guess I confused it with COMDAT sections.


More information about the Digitalmars-d mailing list