summaryrefslogtreecommitdiff
path: root/compiler/systems/t_aix.pas
blob: beb6eec4172a00d9affd8a8f72bf345e66c41b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
{
    Copyright (c) 2011 by Jonas Maebe

    This unit implements support import,export,link routines
    for the AIX target

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}
unit t_aix;

{$i fpcdefs.inc}

interface

  uses
    aasmdata,
    symsym,symdef,ppu,
    import,export,expunix,link;

  type
    timportlibaix=class(timportlib)
      procedure generatelib;override;
    end;

    texportlibaix=class(texportlibunix)
      procedure setfininame(list: TAsmList; const s: string); override;
    end;

    TLinkerAIX=class(texternallinker)
    private
      prtobj  : string[80];
      Function  WriteResponseFile(isdll:boolean) : Boolean;
    public
      constructor Create;override;
      procedure SetDefaultInfo;override;
      function  MakeExecutable:boolean;override;
      function  MakeSharedLibrary:boolean;override;
    end;


implementation

  uses
    SysUtils,
    cutils,cfileutl,cclasses,
    verbose,systems,globtype,globals,
    symconst,script,
    fmodule,
    aasmbase,aasmtai,aasmcpu,cpubase,
    cgbase,cgobj,cgutils,ogbase,ncgutil,
    comprsrc,
    rescmn, i_aix
    ;

{*****************************************************************************
                               timportlibaix
*****************************************************************************}

    procedure timportlibaix.generatelib;
      var
        i : longint;
        ImportLibrary : TImportLibrary;
      begin
        for i:=0 to current_module.ImportLibraryList.Count-1 do
          begin
            ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
            current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
          end;
      end;


{*****************************************************************************
                               texportlibaix
*****************************************************************************}

    procedure texportlibaix.setfininame(list: TAsmList; const s: string);
      begin
        inherited setfininame(list,s);
      end;

{*****************************************************************************
                                  TLinkerAIX
*****************************************************************************}

Constructor TLinkerAIX.Create;
begin
  Inherited Create;
  if not Dontlinkstdlibpath then
    if not(cs_profile in current_settings.moduleswitches) then
      LibrarySearchPath.AddPath(sysrootpath,'/usr/lib;/usr/X11R6/lib;/opt/freeware/lib',true)
    else
      LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/profiled;/usr/X11R6/lib;/opt/freeware/lib',true)
end;


procedure TLinkerAIX.SetDefaultInfo;
begin
  with Info do
   begin
     { the -bpT and -bpD options set the base addresses for text and data
       sections. They are required because otherwise they are both 0 and hence
       overlap, which confuses gdb. GCC uses those same options. -btextro makes
       sure that the binary does not contain any relocations in the text
       section (otherwise you get an error at load time instead of at link time
       in case something is wrong) }
     ExeCmd[1]:='ld -bpT:0x10000000 -bpD:0x20000000 -btextro $OPT $STRIP -L. -o $EXE $CATRES' {$ifdef powerpc64}+' -b64'{$endif};
     DllCmd[1]:='ld -bpT:0x10000000 -bpD:0x20000000 -btextro $OPT $INITFINI $STRIP -G -L. -o $EXE $CATRES' {$ifdef powerpc64}+' -b64'{$endif};
     if cs_debuginfo in current_settings.moduleswitches then
       begin
         { debugging helpers }
         ExeCmd[1]:=ExeCmd[1]+' -lg -bexport:/usr/lib/libg.exp';
         DllCmd[1]:=DllCmd[1]+' -lg -bexport:/usr/lib/libg.exp';
       end;
   end;
{$if defined(powerpc)}
   if not(cs_profile in current_settings.moduleswitches) then
     prtobj:=sysrootpath+'/lib/crt0.o'
   else
     prtobj:=sysrootpath+'/lib/gcrt0.o';
{$elseif defined(powerpc64)}
  if not(cs_profile in current_settings.moduleswitches) then
    prtobj:=sysrootpath+'/lib/crt0_64.o'
  else
    prtobj:=sysrootpath+'/lib/gcrt0_64.o';
{$else}
{$error unsupported AIX architecture}
{$endif}
end;


Function TLinkerAIX.WriteResponseFile(isdll:boolean) : Boolean;
Var
  linkres      : TLinkRes;
  i            : longint;
  HPath        : TCmdStrListItem;
  s,s1         : TCmdStr;
  assumebinutils : boolean;
begin
  result:=False;
  assumebinutils:=
    not(cs_link_on_target in current_settings.globalswitches) and
    not(source_info.system in systems_aix) ;
  { Open link.res file }
  LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,assumebinutils);
  with linkres do
    begin
      { Write path to search libraries }
      HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
      while assigned(HPath) do
       begin
         if assumebinutils then
           Add('SEARCH_DIR("'+HPath.Str+'")')
         else
           Add('-L'+HPath.Str);
         HPath:=TCmdStrListItem(HPath.Next);
       end;
      HPath:=TCmdStrListItem(LibrarySearchPath.First);
      while assigned(HPath) do
       begin
         if assumebinutils then
           Add('SEARCH_DIR("'+HPath.Str+'")')
         else
           Add('-L'+HPath.Str);
         HPath:=TCmdStrListItem(HPath.Next);
       end;

       if assumebinutils then
        StartSection('INPUT(');
      { add objectfiles, start with prt0 always }
       if assumebinutils then
         AddFileName(maybequoted(FindObjectFile(prtobj,'',false)))
       else
         AddFileName(FindObjectFile(prtobj,'',false));
      { main objectfiles }
      while not ObjectFiles.Empty do
       begin
         s:=ObjectFiles.GetFirst;
         if s<>'' then
          if assumebinutils then
            AddFileName(maybequoted(s))
          else
            AddFileName(s)
       end;

      { Write staticlibraries }
      if not StaticLibFiles.Empty then
       begin
         While not StaticLibFiles.Empty do
          begin
            S:=StaticLibFiles.GetFirst;
            if assumebinutils then
              AddFileName(maybequoted(s))
            else
              AddFileName(s);
          end;
       end;

      { Write sharedlibraries like -l<lib> }
      While not SharedLibFiles.Empty do
        begin
          S:=SharedLibFiles.GetFirst;
          i:=Pos(target_info.sharedlibext,S);
          if i>0 then
            Delete(S,i,255);
          Add('-l'+s);
        end;
       { when we have -static for the linker the we also need libgcc }
       if (cs_link_staticflag in current_settings.globalswitches) then
         begin
           Add('-lgcc');
           if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
             Add('-lgcc_eh');
         end;
       if assumebinutils then
         EndSection(')');

      { Write and Close response }
      writetodisk;
      Free;
    end;

  WriteResponseFile:=True;
end;


function TLinkerAIX.MakeExecutable:boolean;
var
  linkscript: TAsmScript;
  binstr,
  cmdstr  : TCmdStr;
  success : boolean;
  StripStr   : string[40];
begin
  if not(cs_link_nolink in current_settings.globalswitches) then
   Message1(exec_i_linking,current_module.exefilename);

  linkscript:=nil;
{ Create some replacements }
  StripStr:='';
  if (cs_link_strip in current_settings.globalswitches) and
     not(cs_link_separate_dbg_file in current_settings.globalswitches) then
   StripStr:='-s';
  if (cs_link_map in current_settings.globalswitches) then
   StripStr:='-bmap:'+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
{ Write used files and libraries }
  WriteResponseFile(false);

{ Call linker }
  SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  binstr:=FindUtil(utilsprefix+BinStr);
  Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  Replace(cmdstr,'$OPT',Info.ExtraOptions);
  { the native AIX linker does not support linkres files, so we need
    CatFileContent(). The binutils cross-linker does support such files, so
    use them when cross-compiling to avoid overflowing the Windows maximum
    command line length
  }
  if not(cs_link_on_target in current_settings.globalswitches) and
     not(source_info.system in systems_aix) then
    Replace(cmdstr,'$CATRES',outputexedir+Info.ResName)
  else
    Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  Replace(cmdstr,'$STRIP',StripStr);

  { create dynamic symbol table? }
  if HasExports then
    cmdstr:=cmdstr+' -E';

  { custom sysroot? (for cross-compiling -> assume gnu ld) }
  if sysrootpath<>'' then
    cmdstr:=cmdstr+' --sysroot='+sysrootpath;

  if not(cs_link_nolink in current_settings.globalswitches) and
     not(tf_no_backquote_support in source_info.flags) then
    begin
      { we have to use a script to use the IFS hack }
      linkscript:=GenerateScript(outputexedir+'ppaslink');
      linkscript.AddLinkCommand(binstr,CmdStr,'');
//      if (extdbgbinstr<>'') then
//        linkscript.AddLinkCommand(extdbgbinstr,extdbgcmdstr,'');
      linkscript.WriteToDisk;
      BinStr:=linkscript.fn;
      if not path_absolute(BinStr) then
        if cs_link_on_target in current_settings.globalswitches then
          BinStr:='.'+target_info.dirsep+BinStr
        else
          BinStr:='.'+source_info.dirsep+BinStr;
      CmdStr:='';
    end;

  success:=DoExec(BinStr,cmdstr,true,true);

  { Remove ReponseFile }
  if (success) and not(cs_link_nolink in current_settings.globalswitches) then
    begin
      DeleteFile(outputexedir+Info.ResName);
      if assigned(linkscript) then
        DeleteFile(linkscript.fn);
    end;
  linkscript.free;

  MakeExecutable:=success;   { otherwise a recursive call to link method }
end;


Function TLinkerAIX.MakeSharedLibrary:boolean;
const
{$ifdef cpu64bitaddr}
  libobj = 'shr_64.o';
{$else}
  libobj = 'shr.o';
{$endif}
var
  linkscript  : TAsmScript;
  exportedsyms: text;
  InitFiniStr : string[80];
  StripStr,
  binstr,
  cmdstr,
  libfn: TCmdStr;
  success : boolean;
begin
  MakeSharedLibrary:=false;
  if not(cs_link_nolink in current_settings.globalswitches) then
   Message1(exec_i_linking,current_module.sharedlibfilename);

{ Write used files and libraries }
  WriteResponseFile(true);

 { Create some replacements }
  InitFiniStr:='-binitfini:'+exportlib.initname+':'+exportlib.fininame;
  Replace(InitFiniStr,'$','.');
  if cs_link_strip in current_settings.globalswitches then
    StripStr:='-s'
  else
    StripStr:='';

{ Call linker }
  SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  binstr:=FindUtil(utilsprefix+BinStr);
  { on AIX, shared libraries are special object files that are stored inside
    an archive. In that archive, the 32 bit version of the library is called
    shr.o and the 64 bit version shr_64.o }
  Replace(cmdstr,'$EXE',maybequoted(libobj));
  Replace(cmdstr,'$OPT',Info.ExtraOptions);
  if not(cs_link_on_target in current_settings.globalswitches) and
     not(source_info.system in systems_aix) then
    Replace(cmdstr,'$CATRES',outputexedir+Info.ResName)
  else
    Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  Replace(cmdstr,'$INITFINI',InitFiniStr);
  Replace(cmdstr,'$STRIP',StripStr);
  { exported symbols }
  if not texportlibunix(exportlib).exportedsymnames.empty then
    begin
      assign(exportedsyms,outputexedir+'linksyms.fpc');
      rewrite(exportedsyms);
      repeat
        writeln(exportedsyms,texportlibunix(exportlib).exportedsymnames.getfirst);
      until texportlibunix(exportlib).exportedsymnames.empty;
      close(exportedsyms);
      cmdstr:=cmdstr+' -bE:'+maybequoted(outputexedir)+'linksyms.fpc';
    end;

  libfn:=maybequoted(current_module.sharedlibfilename);
  { we have to use a script to use the IFS hack }
  linkscript:=GenerateScript(outputexedir+'ppaslink');
  linkscript.AddLinkCommand(binstr,CmdStr,'');
  { delete the target static library containing the dynamic object file in
    case it already existed }
  if FileExists(libfn,true) then
    linkscript.AddDeleteCommand(libfn);
  { and create the new one }
  linkscript.AddLinkCommand(FindUtil(utilsprefix+'ar'),' -X32_64 -q '+libfn+(' '+libobj),'');
  linkscript.WriteToDisk;
  BinStr:=linkscript.fn;
  if not path_absolute(BinStr) then
    if cs_link_on_target in current_settings.globalswitches then
      BinStr:='.'+target_info.dirsep+BinStr
    else
      BinStr:='.'+source_info.dirsep+BinStr;
  CmdStr:='';

  success:=DoExec(BinStr,cmdstr,true,true);

  { Remove ReponseFile }
  if (success) and not(cs_link_nolink in current_settings.globalswitches) then
    begin
      DeleteFile(libobj);
      DeleteFile(outputexedir+Info.ResName);
      DeleteFile(outputexedir+'linksyms.fpc');
      DeleteFile(linkscript.fn);
    end;
  linkscript.free;
  MakeSharedLibrary:=success;   { otherwise a recursive call to link method }
end;

{*****************************************************************************
                                  Initialize
*****************************************************************************}

initialization
  RegisterLinker(ld_aix,TLinkerAIX);
{$ifdef powerpc}
  RegisterImport(system_powerpc_aix,timportlibaix);
  RegisterExport(system_powerpc_aix,texportlibaix);
  RegisterTarget(system_powerpc_aix_info);
{$endif powerpc}
{$ifdef powerpc64}
  RegisterImport(system_powerpc64_aix,timportlibaix);
  RegisterExport(system_powerpc64_aix,texportlibaix);
  RegisterTarget(system_powerpc64_aix_info);
{$endif powerpc64}
  RegisterRes(res_xcoff_info,TWinLikeResourceFile);
end.