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
|
{
Copyright (c) 1998-2006 by Peter Vreman
Includes ELF-related code specific to i386
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,cclasses,
verbose,elfbase,
systems,aasmbase,ogbase,ogelf,assemble;
type
TElfExeOutput386=class(TElfExeOutput)
private
procedure MaybeWriteGOTEntry(reltyp:byte;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_386_NONE = 0;
R_386_32 = 1; { ordinary absolute relocation }
R_386_PC32 = 2; { PC-relative relocation }
R_386_GOT32 = 3; { an offset into GOT }
R_386_PLT32 = 4; { a PC-relative offset into PLT }
R_386_COPY = 5;
R_386_GLOB_DAT = 6;
R_386_JUMP_SLOT = 7;
R_386_RELATIVE = 8;
R_386_GOTOFF = 9; { an offset from GOT base }
R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
R_386_TLS_TPOFF = 14;
R_386_TLS_IE = 15;
R_386_TLS_GOTIE = 16;
R_386_TLS_LE = 17;
R_386_TLS_GD = 18;
R_386_TLS_LDM = 19;
R_386_16 = 20;
R_386_PC16 = 21;
R_386_8 = 22;
R_386_PC8 = 23;
R_386_TLS_GD_32 = 24;
R_386_TLS_GD_PUSH = 25;
R_386_TLS_GD_CALL = 26;
R_386_TLS_GD_POP = 27;
R_386_TLS_LDM_32 = 28;
R_386_TLS_LDM_PUSH = 29;
R_386_TLS_LDM_CALL = 30;
R_386_TLS_LDM_POP = 31;
R_386_TLS_LDO_32 = 32;
R_386_TLS_IE_32 = 33;
R_386_TLS_LE_32 = 34;
R_386_TLS_DTPMOD32 = 35;
R_386_TLS_DTPOFF32 = 36;
R_386_TLS_TPOFF32 = 37;
{ 38 is unused }
R_386_TLS_GOTDESC = 39;
R_386_TLS_DESC_CALL = 40;
R_386_TLS_DESC = 41;
R_386_IRELATIVE = 42;
R_386_GNU_VTINHERIT = 250;
R_386_GNU_VTENTRY = 251;
{****************************************************************************
ELF Target methods
****************************************************************************}
function elf_i386_encodereloc(objrel:TObjRelocation):byte;
begin
case objrel.typ of
RELOC_NONE :
result:=R_386_NONE;
RELOC_RELATIVE :
result:=R_386_PC32;
RELOC_ABSOLUTE :
result:=R_386_32;
RELOC_GOT32 :
result:=R_386_GOT32;
RELOC_GOTPC :
result:=R_386_GOTPC;
RELOC_PLT32 :
result:=R_386_PLT32;
RELOC_GOTOFF:
result:=R_386_GOTOFF;
else
result:=0;
InternalError(2012082301);
end;
end;
procedure elf_i386_loadreloc(objrel:TObjRelocation);
begin
end;
function elf_i386_relocname(reltyp:byte):string;
begin
result:='TODO';
end;
{****************************************************************************
TElfExeOutput386
****************************************************************************}
procedure TElfExeOutput386.WriteFirstPLTEntry;
begin
if IsSharedLibrary then
// push 4(%ebx); jmp *8(%ebx)
pltobjsec.writeBytes(#$FF#$B3#$04#$00#$00#$00#$FF#$A3#$08#$00#$00#$00)
else
begin
pltobjsec.writeBytes(#$FF#$35); // push got+4
pltobjsec.writeReloc_internal(gotpltobjsec,sizeof(pint),4,RELOC_ABSOLUTE);
pltobjsec.writeBytes(#$FF#$25); // jmp *got+8
pltobjsec.writeReloc_internal(gotpltobjsec,2*sizeof(pint),4,RELOC_ABSOLUTE);
end;
pltobjsec.writeBytes(#$90#$90#$90#$90); // nop
end;
procedure TElfExeOutput386.WritePLTEntry(exesym:TExeSymbol);
var
got_offset: aword;
tmp:pint;
begin
got_offset:=gotpltobjsec.size;
if IsSharedLibrary then
begin
pltobjsec.writeBytes(#$FF#$A3); // jmp got+x(%ebx)
pltobjsec.write(got_offset,4);
end
else
begin
pltobjsec.writeBytes(#$FF#$25); // jmp *got+x
pltobjsec.writeReloc_internal(gotpltobjsec,got_offset,4,RELOC_ABSOLUTE);
end;
pltobjsec.writeBytes(#$68); // push $index
tmp:=pltrelocsec.size;
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 .rel.plt entry }
pltrelocsec.writeReloc_internal(gotpltobjsec,got_offset,sizeof(pint),RELOC_ABSOLUTE);
got_offset:=(exesym.dynindex shl 8) or R_386_JUMP_SLOT;
pltrelocsec.write(got_offset,sizeof(pint));
if ElfTarget.relocs_use_addend then
pltrelocsec.writezeros(sizeof(pint));
end;
procedure TElfExeOutput386.WriteIndirectPLTEntry(exesym:TExeSymbol);
begin
// TODO
inherited WriteIndirectPLTEntry(exesym);
end;
procedure TElfExeOutput386.GOTRelocPass1(objsec:TObjSection;var idx:longint);
var
objsym:TObjSymbol;
objreloc:TObjRelocation;
reltyp:byte;
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_386_PLT32:
begin
objsym:=objreloc.symbol.exesymbol.ObjSymbol;
objsym.refs:=objsym.refs or symref_plt;
end;
R_386_32:
if (oso_executable in objsec.SecOptions) or
not (oso_write in objsec.SecOptions) then
begin
if assigned(objreloc.symbol) and assigned(objreloc.symbol.exesymbol) then
begin
objsym:=objreloc.symbol.exesymbol.ObjSymbol;
objsym.refs:=objsym.refs or symref_from_text;
end;
end;
end;
case reltyp of
R_386_TLS_IE:
begin
AllocGOTSlot(objreloc.symbol);
end;
R_386_GOT32:
begin
AllocGOTSlot(objreloc.symbol);
end;
R_386_32:
begin
{ TODO: How to handle absolute relocation to *weak* external symbol
from executable? See test/tweaklib2, symbol test2, ld handles it
differently for PIC and non-PIC code. In non-PIC code it drops
dynamic relocation altogether. }
if not IsSharedLibrary then
exit;
if (oso_executable in objsec.SecOptions) or
not (oso_write in objsec.SecOptions) then
hastextrelocs:=True;
dynrelocsec.alloc(dynrelocsec.shentsize);
objreloc.flags:=objreloc.flags or rf_dynamic;
end;
R_386_PC32:
begin
if not IsSharedLibrary then
exit;
{ In shared library PC32 reloc to external symbol cannot be redirected
to PLT entry, because PIC PLT relies on ebx register set properly. }
if assigned(objreloc.symbol) and
(
(objreloc.symbol.objsection=nil) or
(oso_plt in objreloc.symbol.objsection.SecOptions)
) then
begin
{ Must be a dynamic symbol }
if not (assigned(objreloc.symbol.exesymbol) and
(objreloc.symbol.exesymbol.dynindex<>0)) then
InternalError(2012101201);
if (oso_executable in objsec.SecOptions) or
not (oso_write in objsec.SecOptions) then
hastextrelocs:=True;
dynrelocsec.alloc(dynrelocsec.shentsize);
objreloc.flags:=objreloc.flags or rf_dynamic;
end;
end;
end;
end;
procedure TElfExeOutput386.MaybeWriteGOTEntry(reltyp:byte;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
begin
if (reltyp=R_386_TLS_IE) then
if IsSharedLibrary then
WriteDynRelocEntry(tmp,R_386_TLS_TPOFF,objsym.exesymbol.dynindex,0)
else
else
WriteDynRelocEntry(tmp,R_386_GLOB_DAT,objsym.exesymbol.dynindex,0)
end
else if IsSharedLibrary then
WriteDynRelocEntry(tmp,R_386_RELATIVE,0,relocval);
end;
end;
procedure TElfExeOutput386.DoRelocationFixup(objsec:TObjSection);
var
i,zero:longint;
objreloc: TObjRelocation;
address,
relocval : aint;
relocsec : TObjSection;
data: TDynamicArray;
reltyp: 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 ElfTarget.relocs_use_addend then
address:=objreloc.orgsize
else
begin
data.Seek(objreloc.dataoffset);
data.Read(address,4);
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 .rel.dyn section }
if (relocsec=nil) or (relocsec.used) then
case reltyp of
R_386_PC32:
begin
if (objreloc.flags and rf_dynamic)<>0 then
WriteDynRelocEntry(PC,R_386_PC32,objreloc.symbol.exesymbol.dynindex,0)
else
address:=address+relocval-PC;
end;
R_386_PLT32:
begin
{ If target is in current object, treat as RELOC_RELATIVE }
address:=address+relocval-PC;
end;
R_386_32:
begin
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
begin
address:=address+relocval;
WriteDynRelocEntry(PC,R_386_RELATIVE,0,address);
end
else
{ Don't modify address in this case, as it serves as addend for RTLD }
WriteDynRelocEntry(PC,R_386_32,objreloc.symbol.exesymbol.dynindex,0);
end
else
address:=address+relocval;
end;
R_386_GOTPC:
begin
address:=address+gotsymbol.address-PC;
end;
R_386_GOT32:
begin
MaybeWriteGOTEntry(reltyp,relocval,objreloc.symbol);
relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint)-gotsymbol.address;
address:=address+relocval;
end;
R_386_GOTOFF:
begin
address:=address+relocval-gotsymbol.address;
end;
R_386_TLS_IE:
begin
relocval:=-(tlsseg.MemPos+tlsseg.MemSize-relocval);
MaybeWriteGOTEntry(reltyp,relocval,objreloc.symbol);
{ Resolves to *absolute* offset of GOT slot }
relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
address:=address+relocval;
end;
R_386_TLS_LE_32,
R_386_TLS_LE:
begin
if IsSharedLibrary then
begin
{
if reltyp=R_386_TLS_LE_32 then
begin
WriteDynRelocEntry(PC,R_386_TLS_TPOFF32,symbol.exesymbol.dynindex,0);
address:=tlsseg.MemPos-relocval;
end;
else
begin
WriteDynRelocEntry(PC,R_386_TLS_TPOFF,symbol.exesymbol.dynindex,0);
address:=address-tlsseg.MemPos;
end;
}
end
else if (reltyp=R_386_TLS_LE) then
address:=-(tlsseg.MemPos+tlsseg.MemSize-relocval)
else
address:=tlsseg.MemPos+tlsseg.MemSize-relocval;
end;
else
begin
writeln(reltyp);
internalerror(200604014);
end;
end
else { not relocsec.Used }
address:=0; { Relocation in debug section points to unused section, which is eliminated by linker }
data.Seek(objreloc.dataoffset);
data.Write(address,4);
end;
end;
{*****************************************************************************
Initialize
*****************************************************************************}
const
elf_target_i386 : TElfTarget =
(
max_page_size: $1000;
exe_image_base: $8048000;
machine_code: EM_386;
relocs_use_addend: false;
dyn_reloc_codes: (
R_386_RELATIVE,
R_386_GLOB_DAT,
R_386_JUMP_SLOT,
R_386_COPY,
R_386_IRELATIVE
);
relocname: @elf_i386_relocName;
encodereloc: @elf_i386_encodeReloc;
loadreloc: @elf_i386_loadReloc;
loadsection: nil;
);
as_i386_elf32_info : tasminfo =
(
id : as_i386_elf32;
idtxt : 'ELF';
asmbin : '';
asmcmd : '';
supported_targets : [system_i386_linux,system_i386_beos,
system_i386_freebsd,system_i386_haiku,
system_i386_openbsd,system_i386_netbsd,
system_i386_Netware,system_i386_netwlibc,
system_i386_solaris,system_i386_embedded,
system_i386_android,system_i386_aros];
flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
labelprefix : '.L';
comment : '';
dollarsign: '$';
);
initialization
RegisterAssembler(as_i386_elf32_info,TElfAssembler);
ElfExeOutputClass:=TElfExeOutput386;
ElfTarget:=elf_target_i386;
end.
|