summaryrefslogtreecommitdiff
path: root/compiler/x86_64/cpuelf.pas
blob: 1d27bf8e8eb37cc0320cdf84e8852b51354efc2e (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
{
    Copyright (c) 1998-2006 by Peter Vreman

    Includes ELF-related code specific to x86_64

    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 cpuelf;

{$i fpcdefs.inc}

interface

implementation

  uses
    globtype,cutils,cclasses,
    verbose,elfbase,
    systems,aasmbase,ogbase,ogelf,assemble;

  type
    TElfExeOutputx86_64=class(TElfExeOutput)
    private
      procedure MaybeWriteGOTEntry(relocval:aint;objsym:TObjSymbol);
      procedure MaybeWriteTLSIEGotEntry(relocval:aint;objsym:TObjSymbol);
    protected
      procedure WriteFirstPLTEntry;override;
      procedure WritePLTEntry(exesym:TExeSymbol);override;
      procedure WriteIndirectPLTEntry(exesym:TExeSymbol);override;
      procedure GOTRelocPass1(objsec:TObjSection;var idx:longint);override;
      procedure DoRelocationFixup(objsec:TObjSection);override;
    end;

  const
    { Relocation types }
    R_X86_64_NONE = 0;
    R_X86_64_64 = 1;                 { Direct 64 bit   }
    R_X86_64_PC32 = 2;               { PC relative 32 bit signed  }
    R_X86_64_GOT32 = 3;              { 32 bit GOT entry  }
    R_X86_64_PLT32 = 4;              { 32 bit PLT address  }
    R_X86_64_COPY = 5;               { Copy symbol at runtime  }
    R_X86_64_GLOB_DAT = 6;           { Create GOT entry  }
    R_X86_64_JUMP_SLOT = 7;          { Create PLT entry  }
    R_X86_64_RELATIVE = 8;           { Adjust by program base  }
    R_X86_64_GOTPCREL = 9;           { 32 bit signed PC relative offset to GOT  }
    R_X86_64_32 = 10;                { Direct 32 bit zero extended  }
    R_X86_64_32S = 11;               { Direct 32 bit sign extended  }
    R_X86_64_16 = 12;                { Direct 16 bit zero extended  }
    R_X86_64_PC16 = 13;              { 16 bit sign extended PC relative  }
    R_X86_64_8 = 14;                 { Direct 8 bit sign extended   }
    R_X86_64_PC8 = 15;               { 8 bit sign extended PC relative  }
    R_X86_64_DTPMOD64 = 16;          { ID of module containing symbol  }
    R_X86_64_DTPOFF64 = 17;          { Offset in module's TLS block  }
    R_X86_64_TPOFF64 = 18;           { Offset in initial TLS block  }
    { 32 bit signed PC relative offset to two GOT entries for GD symbol  }
    R_X86_64_TLSGD = 19;
    { 32 bit signed PC relative offset to two GOT entries for LD symbol  }
    R_X86_64_TLSLD = 20;
    R_X86_64_DTPOFF32 = 21;          { Offset in TLS block  }
    { 32 bit signed PC relative offset to GOT entry for IE symbol  }
    R_X86_64_GOTTPOFF = 22;
    R_X86_64_TPOFF32 = 23;           { Offset in initial TLS block  }
    R_X86_64_PC64 = 24;              { PC relative 64-bit signed }
    R_X86_64_GOTOFF64 = 25;          { 64-bit offset from GOT base }
    R_X86_64_GOTPC32 = 26;           { PC-relative offset GOT }
    R_X86_64_GOT64  = 27;            { 64-bit GOT entry offset }
    R_X86_64_GOTPCREL64 = 28;        { 64-bit PC relative offset to GOT entry }
    R_X86_64_GOTPC64 = 29;           { 64-bit PC relative offset to GOT }
    R_X86_64_GOTPLT64 = 30;          { Like GOT64, indicates that PLT entry needed }
    R_X86_64_PLTOFF64 = 31;          { 64-bit GOT relative offset to PLT entry }
    R_X86_64_SIZE32 = 32;
    R_X86_64_SIZE64 = 33;
    R_X86_64_GOTPC32_TLSDESC = 34;
    R_X86_64_TLSDESC_CALL = 35;
    R_X86_64_TLSDESC = 36;
    R_X86_64_IRELATIVE = 37;
    R_X86_64_GNU_VTINHERIT = 250;    { GNU extension to record C++ vtable hierarchy }
    R_X86_64_GNU_VTENTRY = 251;      { GNU extension to record C++ vtable member usage }

  type
    TRelocProp=record
      name: PChar;
      size: byte;
    end;

  const
    relocprops: array[0..37] of TRelocProp=(
      (name: 'R_X86_64_NONE';  size:0),
      (name: 'R_X86_64_64';    size:8),
      (name: 'R_X86_64_PC32';  size:4),
      (name: 'R_X86_64_GOT32'; size:4),
      (name: 'R_X86_64_PLT32'; size:4),
      (name: 'R_X86_64_COPY';  size:0),
      (name: 'R_X86_64_GLOB_DAT'; size:8),
      (name: 'R_X86_64_JUMP_SLOT';size:8),
      (name: 'R_X86_64_RELATIVE'; size:8),
      (name: 'R_X86_64_GOTPCREL'; size:4),
      (name: 'R_X86_64_32';    size:4),
      (name: 'R_X86_64_32S';   size:4),
      (name: 'R_X86_64_16';    size:2),
      (name: 'R_X86_64_PC16';  size:2),
      (name: 'R_X86_64_8';     size:1),
      (name: 'R_X86_64_PC8';   size:1),
      (name: 'R_X86_64_DTPMOD64'; size:8),
      (name: 'R_X86_64_DTPOFF64'; size:8),
      (name: 'R_X86_64_TPOFF64';  size:8),
      (name: 'R_X86_64_TLSGD';    size:4),
      (name: 'R_X86_64_TLSLD';    size:4),
      (name: 'R_X86_64_DTPOFF32'; size:4),
      (name: 'R_X86_64_GOTTPOFF'; size:4),
      (name: 'R_X86_64_TPOFF32';  size:4),
      (name: 'R_X86_64_PC64';     size:8),
      (name: 'R_X86_64_GOTOFF64'; size:8),
      (name: 'R_X86_64_GOTPC32';  size:4),
      (name: 'R_X86_64_GOT64';    size:8),
      (name: 'R_X86_64_GOTPCREL64'; size:8),
      (name: 'R_X86_64_GOTPC64';  size:8),
      (name: 'R_X86_64_GOTPLT64'; size:8),
      (name: 'R_X86_64_PLTOFF64'; size:8),
      (name: 'R_X86_64_SIZE32';   size:4),
      (name: 'R_X86_64_SIZE64';   size:8),
      (name: 'R_X86_64_GOTPC32_TLSDESC'; size:4),
      (name: 'R_X86_64_TLSDESC_CALL';    size:0),
      (name: 'R_X86_64_TLSDESC';   size:8),
      (name: 'R_X86_64_IRELATIVE'; size:8)
    );


{****************************************************************************
                              ELF Target methods
****************************************************************************}

  function elf_x86_64_encodereloc(objrel:TObjRelocation):byte;
    begin
      case objrel.typ of
        RELOC_NONE :
          result:=R_X86_64_NONE;
      { Note: 8 and 16-bit relocations are known to be non-conformant with
        AMD64 ABI, so they aren't handled. }
        RELOC_RELATIVE :
          if objrel.size=8 then
            result:=R_X86_64_PC64
          else if objrel.size=4 then
            result:=R_X86_64_PC32
          else
            InternalError(2012061900);
        RELOC_ABSOLUTE :
          if objrel.size=8 then
            result:=R_X86_64_64
          else if objrel.size=4 then
            result:=R_X86_64_32
          else
            InternalError(2012061901);
        RELOC_ABSOLUTE32 :
          result:=R_X86_64_32S;
        RELOC_GOTPCREL :
          result:=R_X86_64_GOTPCREL;
        RELOC_PLT32 :
          result:=R_X86_64_PLT32;
      else
        result:=0;
        InternalError(2012082302);
      end;
    end;


  procedure elf_x86_64_loadreloc(objrel:TObjRelocation);
    begin
    end;


  function elf_x86_64_relocname(reltyp:byte):string;
    begin
      if reltyp<=high(relocprops) then
        result:=relocprops[reltyp].name
      else
        result:='unknown ('+tostr(reltyp)+')';
    end;

{****************************************************************************
                               TELFExeOutputx86_64
****************************************************************************}

  procedure TElfExeOutputx86_64.GOTRelocPass1(objsec:TObjSection;var idx:longint);
    var
      objsym:TObjSymbol;
      objreloc:TObjRelocation;
      reltyp:byte;
      externsym,fromtext:boolean;
    begin
      objreloc:=TObjRelocation(objsec.ObjRelocations[idx]);
      if (ObjReloc.flags and rf_raw)=0 then
        reltyp:=ElfTarget.encodereloc(ObjReloc)
      else
        reltyp:=ObjReloc.ftype;

      case reltyp of
        R_X86_64_PLT32,
        R_X86_64_PLTOFF64,
        R_X86_64_GOTPLT64:
          begin
            if assigned(ObjReloc.symbol) and assigned(ObjReloc.symbol.exesymbol) then
              begin
                objsym:=ObjReloc.symbol.exesymbol.ObjSymbol;
                objsym.refs:=objsym.refs or symref_plt;
              end;
          end;
      end;

      case reltyp of
        R_X86_64_GOTTPOFF:
          begin
            { TLS IE to locally defined symbol, convert into LE so GOT entry isn't needed
              (Is TLS IE allowed in shared libs at all? Yes it is, when lib is accessing
               a threadvar in main program or in other *statically* loaded lib; TLS IE access to
               own threadvars may render library not loadable dynamically) }
(*
            if not (IsSharedLibrary or (sym.dynindex>0)) then
              begin
                if not IsValidIEtoLE(objsec,ObjReloc) then
                  Comment(v_error,'Cannot transform TLS IE to LE');
                TranslateIEtoLE(objsec,ObjReloc);
                ObjReloc.ftype:=R_X86_64_TPOFF32;
                exit;
              end;
*)
            AllocGOTSlot(objreloc.symbol);
          end;

        R_X86_64_GOT32,
        R_X86_64_GOT64,
        R_X86_64_GOTPCREL,
        R_X86_64_GOTPCREL64:
          begin
            if AllocGOTSlot(objreloc.symbol) then
              if IsSharedLibrary and (objreloc.symbol.exesymbol.dynindex=0) then
                Inc(relative_reloc_count);
          end;

        //R_X86_64_TLSGD,
        //R_X86_64_TLSLD:  { TODO: allocate two GOT slots }

        R_X86_64_TPOFF32,
        { R_X86_64_32S cannot be used in DSOs at all }
        R_X86_64_32S:
          if IsSharedLibrary then
            ReportNonDSOReloc(reltyp,objsec,objreloc);

        { R_X86_64_32 is processed by rtld, but binutils accept it in data sections only.
          Relocating against local symbols is tricky: changing into RELATIVE is not possible,
          so it is changed into relocation against section symbol. This requires adding
          the appropriate section symbol to dynamic symtable. BFD also has some obscure logic
          behind, e.g. it uses .text section for symbols from .data section.

          For now, leave this situation unhandled, as 32-bit relocations aren't commonly
          used in 64-bit code. }

        R_X86_64_32:
          if IsSharedLibrary then
            begin
              if (oso_executable in objsec.SecOptions) or
                not (oso_write in objsec.SecOptions) then
                ReportNonDSOReloc(reltyp,objsec,objreloc)
              else
                InternalError(2012092601);
            end;

        R_X86_64_64:
          begin
            fromtext:=(oso_executable in objsec.SecOptions) or
              not (oso_write in objsec.SecOptions);
            externsym:=assigned(objreloc.symbol) and
              assigned(objreloc.symbol.exesymbol) and
              (objreloc.symbol.exesymbol.dynindex<>0);

            if IsSharedLibrary then
              begin
                if fromtext then
                  hastextrelocs:=True;
                dynrelocsec.alloc(dynrelocsec.shentsize);
                objreloc.flags:=objreloc.flags or rf_dynamic;
                if (not externsym) then
                  Inc(relative_reloc_count);
              end
            else if externsym then
              // TODO: R_X86_64_32 and R_X86_64_32S here?
              begin
                objsym:=objreloc.symbol.ExeSymbol.ObjSymbol;
                { If symbol has non-GOT references from readonly sections, then it needs a
                  copy reloc, which eliminates any dynamic relocations to this symbol from
                  writable sections as well. OTOH if it is referenced *only* from writable
                  sections, then it's better not to generate a copy reloc and keep dynamic
                  relocations. The following code is based on assumption that all readonly
                  sections are processed before writable ones (which is true for current
                  segment mapping).
                  For arbitrary segment mapping, this will probably require a separate pass. }
                if fromtext then
                  objsym.refs:=objsym.refs or symref_from_text
                else if (objsym.refs and symref_from_text)=0 then
                  begin
                    dynrelocsec.alloc(dynrelocsec.shentsize);
                    objreloc.flags:=objreloc.flags or rf_dynamic;
                  end;
              end;
          end;
      end;
    end;


  procedure TElfExeOutputx86_64.MaybeWriteGOTEntry(relocval:aint;objsym:TObjSymbol);
    var
      gotoff,tmp:aword;
    begin
      gotoff:=objsym.exesymbol.gotoffset;
      if gotoff=0 then
        InternalError(2012060902);

      { the GOT slot itself, and a dynamic relocation for it }
      { TODO: only data symbols must get here }
      if gotoff=gotobjsec.Data.size+sizeof(pint) then
        begin
          gotobjsec.write(relocval,sizeof(pint));

          tmp:=gotobjsec.mempos+gotoff-sizeof(pint);
          if (objsym.exesymbol.dynindex>0) then
            dynreloclist.Add(TObjRelocation.CreateRaw(tmp,objsym,R_X86_64_GLOB_DAT))
          else
            if IsSharedLibrary then
              WriteDynRelocEntry(tmp,R_X86_64_RELATIVE,0,relocval);
        end;
    end;


  procedure TElfExeOutputx86_64.MaybeWriteTLSIEGotEntry(relocval:aint;objsym:TObjSymbol);
    var
      gotoff,tmp: aword;
      objrel: TObjRelocation;
    begin
      gotoff:=objsym.exesymbol.gotoffset;
      if gotoff=0 then
        InternalError(2012060903);

      if gotoff=gotobjsec.Data.size+sizeof(pint) then
        begin
          tmp:=gotobjsec.mempos+gotoff-sizeof(pint);
          if (objsym.exesymbol.dynindex>0) then
            begin
              gotobjsec.writezeros(sizeof(pint));
              dynreloclist.Add(TObjRelocation.CreateRaw(tmp,objsym,R_X86_64_TPOFF64));
            end
          else
            begin
              gotobjsec.write(relocval,sizeof(pint));
              if IsSharedLibrary then
                begin
                  objrel:=TObjRelocation.CreateRaw(tmp,nil,R_X86_64_TPOFF64);
                  objrel.orgsize:=relocval;
                  dynreloclist.Add(objrel);
                end;
            end;
        end;
    end;

  procedure TElfExeOutputx86_64.DoRelocationFixup(objsec:TObjSection);
    var
      i,zero:longint;
      objreloc: TObjRelocation;
      address,
      relocval : aint;
      relocsec : TObjSection;
      data: TDynamicArray;
      reltyp,relsize: byte;
      PC: aword;
    begin
      data:=objsec.data;
      for i:=0 to objsec.ObjRelocations.Count-1 do
        begin
          objreloc:=TObjRelocation(objsec.ObjRelocations[i]);
          case objreloc.typ of
            RELOC_NONE:
              continue;
            RELOC_ZERO:
              begin
                data.Seek(objreloc.dataoffset);
                zero:=0;
                data.Write(zero,4);
                continue;
              end;
          end;

          if (objreloc.flags and rf_raw)=0 then
            reltyp:=ElfTarget.encodereloc(objreloc)
          else
            reltyp:=objreloc.ftype;

          if reltyp<=high(relocprops) then
            relsize:=relocprops[reltyp].size
          else
            InternalError(2012092103);

          if ElfTarget.relocs_use_addend then
            address:=objreloc.orgsize
          else
            begin
              data.Seek(objreloc.dataoffset);
              data.Read(address,relsize);
            end;
          if assigned(objreloc.symbol) then
            begin
              relocsec:=objreloc.symbol.objsection;
              relocval:=objreloc.symbol.address;
            end
          else if assigned(objreloc.objsection) then
            begin
              relocsec:=objreloc.objsection;
              relocval:=objreloc.objsection.mempos
            end
          else
            internalerror(2012060702);

          { Only debug sections are allowed to have relocs pointing to unused sections }
          if assigned(relocsec) and not (relocsec.used and assigned(relocsec.exesection)) and
             not (oso_debug in objsec.secoptions) then
            begin
              writeln(objsec.fullname,' references ',relocsec.fullname);
              internalerror(2012060703);
            end;

          PC:=objsec.mempos+objreloc.dataoffset;
          { TODO: if relocsec=nil, relocations must be copied to .rela.dyn section }
          if (relocsec=nil) or (relocsec.used) then
            case reltyp of
              R_X86_64_PC32,
              R_X86_64_PC64:
                begin
                  // TODO: ld rejects PC32 relocations to dynamic symbols, they must use @PLT
                  address:=address+relocval-PC;
                end;

              R_X86_64_PLT32:
                begin
                  { If target is in current object, treat as RELOC_RELATIVE }
                  address:=address+relocval-PC;
                end;

              //R_X86_64_DTPOFF64 is possible in data??
              R_X86_64_DTPOFF32:
                begin
                  { In executable it behaves as TPOFF32 (i.e. generates negative offset),
                    but data expressions like ".long foo@dtpoff" resolve to positive offset }
                  if IsSharedLibrary or not (oso_executable in objsec.SecOptions) then
                    address:=address+relocval-tlsseg.MemPos
                  else
                    address:=address+relocval-(tlsseg.MemPos+tlsseg.MemSize);
                end;

              R_X86_64_TPOFF32,
              R_X86_64_TPOFF64:
                address:=address+relocval-(tlsseg.MemPos+tlsseg.MemSize);

              R_X86_64_GOTTPOFF:
                begin
                  if IsSharedLibrary then
                    relocval:=relocval-tlsseg.MemPos
                  else
                    relocval:=relocval-(tlsseg.MemPos+tlsseg.MemSize);

                  MaybeWriteTLSIEGotEntry(relocval,objreloc.symbol);

                  { resolves to PC-relative offset to GOT slot }
                  relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
                  address:=address+relocval-PC;
                end;

              R_X86_64_GOTPCREL,
              R_X86_64_GOTPCREL64:
                begin
                  MaybeWriteGOTEntry(relocval,objreloc.symbol);

                  { resolves to PC-relative offset to GOT slot }
                  relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
                  address:=address+relocval-PC;
                end;

              R_X86_64_32S,
              R_X86_64_32:
                inc(address,relocval);

              R_X86_64_64:
                begin
                  inc(address,relocval);

                  if (objreloc.flags and rf_dynamic)<>0 then
                    begin
                      if (objreloc.symbol=nil) or
                         (objreloc.symbol.exesymbol=nil) or
                         (objreloc.symbol.exesymbol.dynindex=0) then
                        WriteDynRelocEntry(PC,R_X86_64_RELATIVE,0,address)
                      else
                        dynreloclist.add(TObjRelocation.CreateRaw(PC,objreloc.symbol,R_X86_64_64));
                    end;
                end;

              R_X86_64_GOTPC32,
              R_X86_64_GOTPC64:
                begin
                  address:=address+gotsymbol.address-PC;
                end;

              R_X86_64_GOT32,
              R_X86_64_GOT64:
                begin
                  MaybeWriteGOTEntry(relocval,objreloc.symbol);

                  relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint)-gotsymbol.address;
                  address:=address+relocval;
                end;

              R_X86_64_GOTOFF64,
              R_X86_64_PLTOFF64:
                begin
                  address:=address+relocval-gotsymbol.address;
                end;

              else
                begin
                  writeln(objreloc.typ);
                  internalerror(200604014);
                end;
            end
          else           { not relocsec.Used }
            address:=0;  { Relocation in debug section points to unused section, which is eliminated by linker }

          case relsize of
            8: ;
            4:
              begin
                case reltyp of
                  R_X86_64_32:
                    if qword(address)>qword($FFFFFFFF) then
                      ReportRelocOverflow(reltyp,objsec,objreloc);
                else
                  if (address>high(longint)) or (address<low(longint)) then
                    ReportRelocOverflow(reltyp,objsec,objreloc);
                end;
              end;
          else
            InternalError(2012101102);
          end;

          data.Seek(objreloc.dataoffset);
          data.Write(address,relsize);
        end;
    end;


  procedure TElfExeOutputx86_64.WriteFirstPLTEntry;
    begin
      pltobjsec.writeBytes(#$FF#$35);         // push got+8(%rip)
      pltobjsec.writeReloc_internal(gotpltobjsec,sizeof(pint),4,RELOC_RELATIVE);
      pltobjsec.writeBytes(#$FF#$25);         // jmp  *got+16(%rip)
      pltobjsec.writeReloc_internal(gotpltobjsec,2*sizeof(pint),4,RELOC_RELATIVE);
      pltobjsec.writeBytes(#$0F#$1F#$40#$00); // nopl  0(%rax)
    end;


  procedure TElfExeOutputx86_64.WritePLTEntry(exesym:TExeSymbol);
    var
      got_offset: aword;
      tmp: pint;
    begin
      pltobjsec.writeBytes(#$FF#$25);       // jmp  *got+x(%rip)
      pltobjsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size,4,RELOC_RELATIVE);
      pltobjsec.writeBytes(#$68);           // push  $index
      tmp:=pltrelocsec.size div pltrelocsec.shentsize;
      pltobjsec.write(tmp,4);

      pltobjsec.writeBytes(#$E9);           // jmp   .plt
      tmp:=-(4+pltobjsec.Size);
      pltobjsec.write(tmp,4);

      { write a .got.plt slot pointing back to the 'push' instruction }
      gotpltobjsec.writeReloc_internal(pltobjsec,pltobjsec.size-(16-6),sizeof(pint),RELOC_ABSOLUTE);

      { write a .rela.plt entry (Elf64_rela record) }
      pltrelocsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size-sizeof(pint),sizeof(pint),RELOC_ABSOLUTE);
      got_offset:=(qword(exesym.dynindex) shl 32) or R_X86_64_JUMP_SLOT;
      pltrelocsec.write(got_offset,sizeof(pint));
      if ElfTarget.relocs_use_addend then
        pltrelocsec.writezeros(sizeof(pint));
    end;


  procedure TElfExeOutputx86_64.WriteIndirectPLTEntry(exesym:TExeSymbol);
    var
      tmp: pint;
      objsym:TObjSymbol;
      targetsym:TObjSymbol;
    begin
      targetsym:=exesym.ObjSymbol;
      objsym:=internalobjdata.CreateSymbol(exesym.name);
      objsym.typ:=AT_FUNCTION;
      objsym.bind:=exesym.ObjSymbol.bind;  { AB_EXTERNAL or AB_WEAK_EXTERNAL }
      objsym.offset:=pltobjsec.size;
      objsym.objsection:=pltobjsec;
      exesym.ObjSymbol:=objsym;

      pltobjsec.writeBytes(#$FF#$25);       // jmp  *got+x(%rip)
      pltobjsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size,4,RELOC_RELATIVE);
      { TODO: Are these entries relevant when linking dynamic?
        (for static linking, they don't matter) }
      pltobjsec.writeBytes(#$68);           // push  $index
      tmp:=pltrelocsec.size div pltrelocsec.shentsize;
      pltobjsec.write(tmp,4);

      pltobjsec.writeBytes(#$E9);           // jmp   .plt
      tmp:=-(4+pltobjsec.Size);
      pltobjsec.write(tmp,4);

      { write a .got.plt slot pointing back to the 'push' instruction }
      gotpltobjsec.writeReloc_internal(pltobjsec,pltobjsec.size-(16-6),sizeof(pint),RELOC_ABSOLUTE);

      { write a .rela.iplt entry (Elf64_rela record) }
      ipltrelocsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size-sizeof(pint),sizeof(pint),RELOC_ABSOLUTE);
      tmp:=R_X86_64_IRELATIVE;
      ipltrelocsec.write(tmp,sizeof(pint));
      if ElfTarget.relocs_use_addend then
        ipltrelocsec.writeReloc_internal(targetsym.objsection,targetsym.offset,sizeof(pint),RELOC_ABSOLUTE);
    end;

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

  const
    elf_target_x86_64: TElfTarget =
      (
        max_page_size:     $200000;
        exe_image_base:    $400000;
        machine_code:      EM_X86_64;
        relocs_use_addend: true;
        dyn_reloc_codes: (
          R_X86_64_RELATIVE,
          R_X86_64_GLOB_DAT,
          R_X86_64_JUMP_SLOT,
          R_X86_64_COPY,
          R_X86_64_IRELATIVE
        );
        relocname:         @elf_x86_64_relocName;
        encodereloc:       @elf_x86_64_encodeReloc;
        loadreloc:         @elf_x86_64_loadReloc;
        loadsection:       nil;
      );


    as_x86_64_elf64_info : tasminfo =
      (
        id     : as_x86_64_elf64;
        idtxt  : 'ELF';
        asmbin : '';
        asmcmd : '';
        supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
                             system_x86_64_openbsd,system_x86_64_netbsd,
                             system_x86_64_dragonfly];
        flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
        labelprefix : '.L';
        comment : '';
        dollarsign: '$';
      );

initialization
  RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  ElfTarget:=elf_target_x86_64;
  ElfExeOutputClass:=TElfExeOutputx86_64;

end.