mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-10 02:28:22 +02:00
+ ELF linker back-ends for ARM and MIPS.
ARM status: roughly corresponds to i386 one, passes the test suite. Handles libraries, can link static libc code including basic PIC and TLS IE/LE stuff. Completely misses Thumb support. Also does not handle ABI-specific stuff, for this reason internally linked .so cannot be used for linking executables with ld. Little-endian only. Tested only on "versatilepb" QEMU virtual machine. MIPS status: can link the compiler and at least some dynamic executables including fpmake. Some PIC support is present but almost untested. Specific header flags and sections are also not handled yet. Written to handle both endian, but tested for big-endian only ("malta" QEMU VM), including cross-linking from x86_64. git-svn-id: trunk@23376 -
This commit is contained in:
parent
af4935e346
commit
32ffddaad8
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -73,6 +73,7 @@ compiler/arm/armreg.dat svneol=native#text/plain
|
||||
compiler/arm/armtab.inc svneol=native#text/plain
|
||||
compiler/arm/cgcpu.pas svneol=native#text/plain
|
||||
compiler/arm/cpubase.pas svneol=native#text/plain
|
||||
compiler/arm/cpuelf.pas svneol=native#text/plain
|
||||
compiler/arm/cpuinfo.pas svneol=native#text/plain
|
||||
compiler/arm/cpunode.pas svneol=native#text/plain
|
||||
compiler/arm/cpupara.pas svneol=native#text/plain
|
||||
@ -319,6 +320,7 @@ compiler/mips/aoptcpub.pas svneol=native#text/plain
|
||||
compiler/mips/aoptcpud.pas svneol=native#text/plain
|
||||
compiler/mips/cgcpu.pas svneol=native#text/plain
|
||||
compiler/mips/cpubase.pas svneol=native#text/plain
|
||||
compiler/mips/cpuelf.pas svneol=native#text/plain
|
||||
compiler/mips/cpugas.pas svneol=native#text/plain
|
||||
compiler/mips/cpuinfo.pas svneol=native#text/plain
|
||||
compiler/mips/cpunode.pas svneol=native#text/plain
|
||||
|
882
compiler/arm/cpuelf.pas
Normal file
882
compiler/arm/cpuelf.pas
Normal file
@ -0,0 +1,882 @@
|
||||
{
|
||||
Copyright (c) 2012 by Sergei Gorelkin
|
||||
|
||||
Includes ELF-related code specific to ARM
|
||||
|
||||
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;
|
||||
|
||||
interface
|
||||
|
||||
{$i fpcdefs.inc}
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
globtype,cutils,cclasses,
|
||||
verbose, elfbase,
|
||||
systems,aasmbase,ogbase,ogelf,assemble;
|
||||
|
||||
type
|
||||
TElfExeOutputARM=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_ARM_NONE = 0;
|
||||
R_ARM_PC24 = 1; // deprecated
|
||||
R_ARM_ABS32 = 2;
|
||||
R_ARM_REL32 = 3;
|
||||
R_ARM_LDR_PC_G0 = 4;
|
||||
R_ARM_ABS16 = 5;
|
||||
R_ARM_ABS12 = 6;
|
||||
R_ARM_THM_ABS5 = 7;
|
||||
R_ARM_ABS8 = 8;
|
||||
R_ARM_SBREL32 = 9;
|
||||
R_ARM_THM_CALL = 10;
|
||||
R_ARM_THM_PC8 = 11;
|
||||
R_ARM_BREL_ADJ = 12;
|
||||
R_ARM_TLS_DESC = 13;
|
||||
{ 14,15,16 are obsolete }
|
||||
R_ARM_TLS_DTPMOD32 = 17;
|
||||
R_ARM_TLS_DTPOFF32 = 18;
|
||||
R_ARM_TLS_TPOFF32 = 19;
|
||||
R_ARM_COPY = 20;
|
||||
R_ARM_GLOB_DAT = 21;
|
||||
R_ARM_JUMP_SLOT = 22;
|
||||
R_ARM_RELATIVE = 23;
|
||||
R_ARM_GOTOFF32 = 24;
|
||||
R_ARM_BASE_PREL = 25;
|
||||
R_ARM_GOT_BREL = 26;
|
||||
R_ARM_PLT32 = 27; // deprecated
|
||||
R_ARM_CALL = 28;
|
||||
R_ARM_JUMP24 = 29;
|
||||
R_ARM_THM_JUMP24 = 30;
|
||||
R_ARM_BASE_ABS = 31;
|
||||
{ 32,33,34 are obsolete }
|
||||
R_ARM_LDR_SBREL_11_0 = 35; // deprecated
|
||||
R_ARM_ALU_SBREL_19_12 = 36; // deprecated
|
||||
R_ARM_ALU_SBREL_27_20 = 37; // deprecated
|
||||
R_ARM_TARGET1 = 38;
|
||||
R_ARM_SBREL31 = 39; // deprecated
|
||||
R_ARM_V4BX = 40;
|
||||
R_ARM_TARGET2 = 41;
|
||||
R_ARM_PREL31 = 42;
|
||||
R_ARM_MOVW_ABS_NC = 43;
|
||||
R_ARM_MOVT_ABS = 44;
|
||||
R_ARM_MOVW_PREL_NC = 45;
|
||||
R_ARM_MOVT_PREL = 46;
|
||||
R_ARM_THM_MOVW_ABS_NC = 47;
|
||||
R_ARM_THM_MOVT_ABS = 48;
|
||||
R_ARM_THM_MOVW_PREL_NC = 49;
|
||||
R_ARM_THM_MOVT_PREL = 50;
|
||||
R_ARM_THM_JUMP19 = 51;
|
||||
R_ARM_THM_JUMP6 = 52;
|
||||
R_ARM_THM_ALU_PREL_11_0 = 53;
|
||||
R_ARM_THM_PC12 = 54;
|
||||
R_ARM_ABS32_NOI = 55;
|
||||
R_ARM_REL32_NOI = 56;
|
||||
R_ARM_ALU_PC_G0_NC = 57;
|
||||
R_ARM_ALU_PC_G0 = 58;
|
||||
R_ARM_ALU_PC_G1_NC = 59;
|
||||
R_ARM_ALU_PC_G1 = 60;
|
||||
R_ARM_ALU_PC_G2 = 61;
|
||||
R_ARM_LDR_PC_G1 = 62;
|
||||
R_ARM_LDR_PC_G2 = 63;
|
||||
R_ARM_LDRS_PC_G0 = 64;
|
||||
R_ARM_LDRS_PC_G1 = 65;
|
||||
R_ARM_LDRS_PC_G2 = 66;
|
||||
R_ARM_LDC_PC_G0 = 67;
|
||||
R_ARM_LDC_PC_G1 = 68;
|
||||
R_ARM_LDC_PC_G2 = 69;
|
||||
R_ARM_ALU_SB_G0_NC = 70;
|
||||
R_ARM_ALU_SB_G0 = 71;
|
||||
R_ARM_ALU_SB_G1_NC = 72;
|
||||
R_ARM_ALU_SB_G1 = 73;
|
||||
R_ARM_ALU_SB_G2 = 74;
|
||||
R_ARM_LDR_SB_G0 = 75;
|
||||
R_ARM_LDR_SB_G1 = 76;
|
||||
R_ARM_LDR_SB_G2 = 77;
|
||||
R_ARM_LDRS_SB_G0 = 78;
|
||||
R_ARM_LDRS_SB_G1 = 79;
|
||||
R_ARM_LDRS_SB_G2 = 80;
|
||||
R_ARM_LDC_SB_G0 = 81;
|
||||
R_ARM_LDC_SB_G1 = 82;
|
||||
R_ARM_LDC_SB_G2 = 83;
|
||||
R_ARM_MOVW_BREL_NC = 84;
|
||||
R_ARM_MOVT_BREL = 85;
|
||||
R_ARM_MOVW_BREL = 86;
|
||||
R_ARM_THM_MOVW_BREL_NC = 87;
|
||||
R_ARM_THM_MOVT_BREL = 88;
|
||||
R_ARM_THM_MOVW_BREL = 89;
|
||||
R_ARM_TLS_GOTDESC = 90;
|
||||
R_ARM_TLS_CALL = 91;
|
||||
R_ARM_TLS_DESCSEQ = 92;
|
||||
R_ARM_THM_TLS_CALL = 93;
|
||||
R_ARM_PLT32_ABS = 94;
|
||||
R_ARM_GOT_ABS = 95;
|
||||
R_ARM_GOT_PREL = 96;
|
||||
R_ARM_GOT_BREL12 = 97;
|
||||
R_ARM_GOTOFF12 = 98;
|
||||
R_ARM_GOTRELAX = 99;
|
||||
R_ARM_GNU_VTENTRY = 100; // deprecated - old C++ abi
|
||||
R_ARM_GNU_VTINHERIT = 101; // deprecated - old C++ abi
|
||||
R_ARM_THM_JUMP11 = 102;
|
||||
R_ARM_THM_JUMP8 = 103;
|
||||
R_ARM_TLS_GD32 = 104;
|
||||
R_ARM_TLS_LDM32 = 105;
|
||||
R_ARM_TLS_LDO32 = 106;
|
||||
R_ARM_TLS_IE32 = 107;
|
||||
R_ARM_TLS_LE32 = 108;
|
||||
R_ARM_TLS_LDO12 = 109;
|
||||
R_ARM_TLS_LE12 = 110;
|
||||
R_ARM_TLS_IE12GP = 111;
|
||||
{ 112-127 are for private experiments }
|
||||
{ 128 is obsolete }
|
||||
R_ARM_THM_TLS_DESCSEQ = 129;
|
||||
R_ARM_IRELATIVE = 160;
|
||||
|
||||
{ Section types }
|
||||
SHT_ARM_EXIDX = $70000001;
|
||||
SHT_ARM_PREEMPTMAP = $70000002;
|
||||
SHT_ARM_ATTRIBUTES = $70000003;
|
||||
SHT_ARM_DEBUGOVERLAY = $70000004;
|
||||
SHT_ARM_OVERLAYSECTION = $70000005;
|
||||
|
||||
TCB_SIZE = 8;
|
||||
|
||||
{ Using short identifiers to save typing. This ARM thing has more relocations
|
||||
than it has instructions... }
|
||||
const
|
||||
g0=1;
|
||||
g1=2;
|
||||
g2=3;
|
||||
gpmask=3;
|
||||
pc=4;
|
||||
nc=8;
|
||||
thm=16;
|
||||
|
||||
type
|
||||
TArmRelocProp=record
|
||||
name: PChar;
|
||||
flags: byte; // bits 0,1: group, bit 2: PC-relative, bit 3: unchecked,
|
||||
// bit 4: THUMB
|
||||
end;
|
||||
|
||||
const
|
||||
relocprops: array[0..111] of TArmRelocProp = (
|
||||
(name: 'R_ARM_NONE'; flags: 0), //
|
||||
(name: 'R_ARM_PC24'; flags: pc), //
|
||||
(name: 'R_ARM_ABS32'; flags: 0), //
|
||||
(name: 'R_ARM_REL32'; flags: pc), //
|
||||
(name: 'R_ARM_LDR_PC_G0'; flags: g0+pc), //
|
||||
(name: 'R_ARM_ABS16'; flags: 0),
|
||||
(name: 'R_ARM_ABS12'; flags: 0),
|
||||
(name: 'R_ARM_THM_ABS5'; flags: thm),
|
||||
(name: 'R_ARM_ABS8'; flags: 0),
|
||||
(name: 'R_ARM_SBREL32'; flags: 0),
|
||||
(name: 'R_ARM_THM_CALL'; flags: thm),
|
||||
(name: 'R_ARM_THM_PC8'; flags: pc+thm),
|
||||
(name: 'R_ARM_BREL_ADJ'; flags: 0),
|
||||
(name: 'R_ARM_TLS_DESC'; flags: 0),
|
||||
(name: 'obsolete(14)'; flags: 0),
|
||||
(name: 'obsolete(15)'; flags: 0),
|
||||
(name: 'obsolete(16)'; flags: 0),
|
||||
(name: 'R_ARM_TLS_DTPMOD32'; flags: 0),
|
||||
(name: 'R_ARM_TLS_DTPOFF32'; flags: 0),
|
||||
(name: 'R_ARM_TLS_TPOFF32'; flags: 0),
|
||||
(name: 'R_ARM_COPY'; flags: 0),
|
||||
(name: 'R_ARM_GLOB_DAT'; flags: 0),
|
||||
(name: 'R_ARM_JUMP_SLOT'; flags: 0),
|
||||
(name: 'R_ARM_RELATIVE'; flags: 0),
|
||||
(name: 'R_ARM_GOTOFF32'; flags: 0),
|
||||
(name: 'R_ARM_BASE_PREL'; flags: pc), //
|
||||
(name: 'R_ARM_GOT_BREL'; flags: 0), //
|
||||
(name: 'R_ARM_PLT32'; flags: pc), //
|
||||
(name: 'R_ARM_CALL'; flags: pc), //
|
||||
(name: 'R_ARM_JUMP24'; flags: pc), //
|
||||
(name: 'R_ARM_THM_JUMP24'; flags: thm),
|
||||
(name: 'R_ARM_BASE_ABS'; flags: 0),
|
||||
(name: 'obsolete(32)'; flags: 0),
|
||||
(name: 'obsolete(33)'; flags: 0),
|
||||
(name: 'obsolete(34)'; flags: 0),
|
||||
(name: 'R_ARM_LDR_SBREL_11_0'; flags: g0),
|
||||
(name: 'R_ARM_ALU_SBREL_19_12'; flags: g1),
|
||||
(name: 'R_ARM_ALU_SBREL_27_20'; flags: g2),
|
||||
(name: 'R_ARM_TARGET1'; flags: 0),
|
||||
(name: 'R_ARM_SBREL31'; flags: 0),
|
||||
(name: 'R_ARM_V4BX'; flags: 0),
|
||||
(name: 'R_ARM_TARGET2'; flags: 0),
|
||||
(name: 'R_ARM_PREL31'; flags: 0),
|
||||
(name: 'R_ARM_MOVW_ABS_NC'; flags: nc),
|
||||
(name: 'R_ARM_MOVT_ABS'; flags: 0),
|
||||
(name: 'R_ARM_MOVW_PREL_NC'; flags: nc),
|
||||
(name: 'R_ARM_MOVT_PREL'; flags: 0),
|
||||
(name: 'R_ARM_THM_MOVW_ABS_NC'; flags: nc+thm),
|
||||
(name: 'R_ARM_THM_MOVT_ABS'; flags: thm),
|
||||
(name: 'R_ARM_THM_MOVW_PREL_NC'; flags: nc+thm),
|
||||
(name: 'R_ARM_THM_MOVT_PREL'; flags: thm),
|
||||
(name: 'R_ARM_THM_JUMP19'; flags: thm),
|
||||
(name: 'R_ARM_THM_JUMP6'; flags: thm),
|
||||
(name: 'R_ARM_THM_ALU_PREL_11_0'; flags: thm+pc),
|
||||
(name: 'R_ARM_THM_PC12'; flags: thm+pc),
|
||||
(name: 'R_ARM_ABS32_NOI'; flags: 0),
|
||||
(name: 'R_ARM_REL32_NOI'; flags: pc),
|
||||
(name: 'R_ARM_ALU_PC_G0_NC'; flags: pc+g0+nc), //
|
||||
(name: 'R_ARM_ALU_PC_G0'; flags: pc+g0), //
|
||||
(name: 'R_ARM_ALU_PC_G1_NC'; flags: pc+g1+nc), //
|
||||
(name: 'R_ARM_ALU_PC_G1'; flags: pc+g1), //
|
||||
(name: 'R_ARM_ALU_PC_G2'; flags: pc+g2), //
|
||||
(name: 'R_ARM_LDR_PC_G1'; flags: pc+g1), //
|
||||
(name: 'R_ARM_LDR_PC_G2'; flags: pc+g2), //
|
||||
(name: 'R_ARM_LDRS_PC_G0'; flags: pc+g0), //
|
||||
(name: 'R_ARM_LDRS_PC_G1'; flags: pc+g1), //
|
||||
(name: 'R_ARM_LDRS_PC_G2'; flags: pc+g2), //
|
||||
(name: 'R_ARM_LDC_PC_G0'; flags: pc+g0), //
|
||||
(name: 'R_ARM_LDC_PC_G1'; flags: pc+g1), //
|
||||
(name: 'R_ARM_LDC_PC_G2'; flags: pc+g2), //
|
||||
(name: 'R_ARM_ALU_SB_G0_NC'; flags: g0+nc), //
|
||||
(name: 'R_ARM_ALU_SB_G0'; flags: g0), //
|
||||
(name: 'R_ARM_ALU_SB_G1_NC'; flags: g1+nc), //
|
||||
(name: 'R_ARM_ALU_SB_G1'; flags: g1), //
|
||||
(name: 'R_ARM_ALU_SB_G2'; flags: g2), //
|
||||
(name: 'R_ARM_LDR_SB_G0'; flags: g0), //
|
||||
(name: 'R_ARM_LDR_SB_G1'; flags: g1), //
|
||||
(name: 'R_ARM_LDR_SB_G2'; flags: g2), //
|
||||
(name: 'R_ARM_LDRS_SB_G0'; flags: g0), //
|
||||
(name: 'R_ARM_LDRS_SB_G1'; flags: g1), //
|
||||
(name: 'R_ARM_LDRS_SB_G2'; flags: g2), //
|
||||
(name: 'R_ARM_LDC_SB_G0'; flags: g0), //
|
||||
(name: 'R_ARM_LDC_SB_G1'; flags: g1), //
|
||||
(name: 'R_ARM_LDC_SB_G2'; flags: g2), //
|
||||
(name: 'R_ARM_MOVW_BREL_NC'; flags: nc),
|
||||
(name: 'R_ARM_MOVT_BREL'; flags: 0),
|
||||
(name: 'R_ARM_MOVW_BREL'; flags: 0),
|
||||
(name: 'R_ARM_THM_MOVW_BREL_NC'; flags: nc+thm),
|
||||
(name: 'R_ARM_THM_MOVT_BREL'; flags: thm),
|
||||
(name: 'R_ARM_THM_MOVW_BREL'; flags: thm),
|
||||
(name: 'R_ARM_TLS_GOTDESC'; flags: 0),
|
||||
(name: 'R_ARM_TLS_CALL'; flags: 0),
|
||||
(name: 'R_ARM_TLS_DESCSEQ'; flags: 0),
|
||||
(name: 'R_ARM_THM_TLS_CALL'; flags: 0),
|
||||
(name: 'R_ARM_PLT32_ABS'; flags: 0),
|
||||
(name: 'R_ARM_GOT_ABS'; flags: 0),
|
||||
(name: 'R_ARM_GOT_PREL'; flags: pc), //
|
||||
(name: 'R_ARM_GOT_BREL12'; flags: 0),
|
||||
(name: 'R_ARM_GOTOFF12'; flags: 0),
|
||||
(name: 'R_ARM_GOTRELAX'; flags: 0),
|
||||
(name: 'R_ARM_GNU_VTENTRY'; flags: 0),
|
||||
(name: 'R_ARM_GNU_VTINHERIT'; flags: 0),
|
||||
(name: 'R_ARM_THM_JUMP11'; flags: thm),
|
||||
(name: 'R_ARM_THM_JUMP8'; flags: thm),
|
||||
(name: 'R_ARM_TLS_GD32'; flags: 0),
|
||||
(name: 'R_ARM_TLS_LDM32'; flags: 0),
|
||||
(name: 'R_ARM_TLS_LDO32'; flags: 0),
|
||||
(name: 'R_ARM_TLS_IE32'; flags: 0),
|
||||
(name: 'R_ARM_TLS_LE32'; flags: 0),
|
||||
(name: 'R_ARM_TLS_LDO12'; flags: 0),
|
||||
(name: 'R_ARM_TLS_LE12'; flags: 0),
|
||||
(name: 'R_ARM_TLS_IE12GP'; flags: 0)
|
||||
);
|
||||
|
||||
{****************************************************************************
|
||||
ELF Target methods
|
||||
****************************************************************************}
|
||||
|
||||
function elf_arm_encodereloc(objrel:TObjRelocation):byte;
|
||||
begin
|
||||
case objrel.typ of
|
||||
RELOC_NONE:
|
||||
result:=R_ARM_NONE;
|
||||
RELOC_ABSOLUTE:
|
||||
result:=R_ARM_ABS32;
|
||||
RELOC_RELATIVE:
|
||||
result:=R_ARM_REL32;
|
||||
else
|
||||
result:=0;
|
||||
InternalError(2012110602);
|
||||
end;
|
||||
end;
|
||||
|
||||
function elf_arm_relocname(reltyp:byte):string;
|
||||
begin
|
||||
if reltyp<=high(relocprops) then
|
||||
result:=relocprops[reltyp].name
|
||||
else
|
||||
case reltyp of
|
||||
112..127:
|
||||
result:='R_ARM_PRIVATE_'+tostr(reltyp-112);
|
||||
R_ARM_THM_TLS_DESCSEQ:
|
||||
result:='R_ARM_THM_TLS_DESCSEQ';
|
||||
R_ARM_IRELATIVE:
|
||||
result:='R_ARM_IRELATIVE';
|
||||
else
|
||||
result:='unknown ('+tostr(reltyp)+')';
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure elf_arm_loadreloc(objrel:TObjRelocation);
|
||||
begin
|
||||
if (objrel.ftype=R_ARM_V4BX) then
|
||||
objrel.flags:=objrel.flags or rf_nosymbol;
|
||||
end;
|
||||
|
||||
function elf_arm_loadsection(objinput:TElfObjInput;objdata:TObjData;const shdr:TElfsechdr;shindex:longint):boolean;
|
||||
var
|
||||
secname:string;
|
||||
begin
|
||||
case shdr.sh_type of
|
||||
SHT_ARM_EXIDX,
|
||||
SHT_ARM_PREEMPTMAP,
|
||||
SHT_ARM_ATTRIBUTES:
|
||||
begin
|
||||
objinput.CreateSection(shdr,shindex,objdata,secname);
|
||||
result:=true;
|
||||
end;
|
||||
else
|
||||
writeln(hexstr(shdr.sh_type,8));
|
||||
result:=false;
|
||||
end;
|
||||
end;
|
||||
|
||||
{****************************************************************************
|
||||
TELFExeOutputARM
|
||||
****************************************************************************}
|
||||
|
||||
function group_reloc_mask(value:longword;n:longint;out final_residual:longword):longword;
|
||||
var
|
||||
i:longint;
|
||||
g_n:longword;
|
||||
shift:longint;
|
||||
begin
|
||||
result:=0;
|
||||
for i:=0 to n do
|
||||
begin
|
||||
if (value=0) then
|
||||
shift:=0
|
||||
else
|
||||
{ MSB in the residual, aligned to a 2-bit boundary }
|
||||
shift:=max(0,(bsrdword(value) and (not 1))-6);
|
||||
|
||||
{ Calculate plain g_n and encode it into constant+rotation form }
|
||||
g_n:=value and ($ff shl shift);
|
||||
result:=(g_n shr shift);
|
||||
if (g_n>$FF) then
|
||||
result:=result or ((32-shift) div 2) shl 8;
|
||||
|
||||
{ Mask away the processed part of residual }
|
||||
value:=value and (not g_n);
|
||||
end;
|
||||
final_residual:=value;
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputARM.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
|
||||
WriteDynRelocEntry(tmp,R_ARM_GLOB_DAT,objsym.exesymbol.dynindex,0)
|
||||
end
|
||||
else if IsSharedLibrary then
|
||||
WriteDynRelocEntry(tmp,R_ARM_RELATIVE,0,relocval);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputARM.WriteFirstPLTEntry;
|
||||
begin
|
||||
pltobjsec.WriteBytes(
|
||||
#$04#$E0#$2D#$E5+ // str lr, [sp, #-4]!
|
||||
#$04#$E0#$9F#$E5+ // ldr lr, [pc, #4]
|
||||
#$0E#$E0#$8F#$E0+ // add lr, pc, lr
|
||||
#$08#$F0#$BE#$E5); // ldr pc, [lr, #8]!
|
||||
// .long _GLOBAL_OFFSET_TABLE-.
|
||||
pltobjsec.writeReloc_internal(gotpltobjsec,0,4,RELOC_RELATIVE);
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputARM.WritePLTEntry(exesym: TExeSymbol);
|
||||
var
|
||||
tmp: longword;
|
||||
sym:TObjSymbol;
|
||||
begin
|
||||
{ TODO: it may be beneficial to postpone processing until after mempos pass,
|
||||
and calculate instructions directly, instead of messing with complex relocations. }
|
||||
{ Group relocation to "section+offset" with REL-style is impossible, because the
|
||||
offset has be encoded into instructions, and it is only possible for offsets
|
||||
representable as shifter constants. Therefore we need to define a symbol
|
||||
(and risk a name conflict, to some degree) }
|
||||
internalobjdata.setsection(gotpltobjsec);
|
||||
sym:=internalobjdata.SymbolDefine(exesym.name+'_ptr',AB_LOCAL,AT_DATA);
|
||||
pltobjsec.WriteBytes(
|
||||
#$08#$C0#$4F#$E2+ // add ip,pc,#:pc_g0_nc:sym-8
|
||||
#$04#$C0#$4C#$E2+ // add ip,ip,#:pc_g1_nc:sym-4
|
||||
#$00#$F0#$BC#$E5); // ldr pc,[ip,#:pc_g2:sym]!
|
||||
|
||||
pltobjsec.addrawReloc(pltobjsec.size-12,sym,R_ARM_ALU_PC_G0_NC);
|
||||
pltobjsec.addrawReloc(pltobjsec.size-8,sym,R_ARM_ALU_PC_G1_NC);
|
||||
pltobjsec.addrawReloc(pltobjsec.size-4,sym,R_ARM_LDR_PC_G2);
|
||||
|
||||
{ .got.plt slot initially points to the first PLT entry }
|
||||
gotpltobjsec.writeReloc_internal(pltobjsec,0,sizeof(pint),RELOC_ABSOLUTE);
|
||||
{ write a .rel.plt entry (Elf32_rel record) }
|
||||
pltrelocsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size-sizeof(pint),sizeof(pint),RELOC_ABSOLUTE);
|
||||
tmp:=(exesym.dynindex shl 8) or R_ARM_JUMP_SLOT;
|
||||
pltrelocsec.write(tmp,sizeof(tmp));
|
||||
if ElfTarget.relocs_use_addend then
|
||||
pltrelocsec.writezeros(sizeof(pint));
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputARM.WriteIndirectPLTEntry(exesym: TExeSymbol);
|
||||
begin
|
||||
inherited WriteIndirectPLTEntry(exesym);
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputARM.GOTRelocPass1(objsec:TObjSection;var idx:longint);
|
||||
var
|
||||
objreloc:TObjRelocation;
|
||||
exesym:TExeSymbol;
|
||||
objsym:TObjSymbol;
|
||||
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
|
||||
// Any call or jump can go through PLT, no x86-like segregation here.
|
||||
R_ARM_PC24,
|
||||
R_ARM_CALL,
|
||||
R_ARM_JUMP24,
|
||||
R_ARM_PREL31,
|
||||
R_ARM_THM_CALL,
|
||||
R_ARM_THM_JUMP24,
|
||||
R_ARM_THM_JUMP19,
|
||||
R_ARM_PLT32:
|
||||
begin
|
||||
if (objreloc.symbol=nil) or (objreloc.symbol.exesymbol=nil) then
|
||||
exit;
|
||||
exesym:=objreloc.symbol.exesymbol;
|
||||
exesym.objsymbol.refs:=exesym.objsymbol.refs or symref_plt;
|
||||
end;
|
||||
|
||||
R_ARM_ABS32:
|
||||
if Assigned(ObjReloc.symbol.exesymbol) then
|
||||
begin
|
||||
objsym:=ObjReloc.symbol.exesymbol.ObjSymbol;
|
||||
if (oso_executable in objsec.SecOptions) or
|
||||
not (oso_write in objsec.SecOptions) then
|
||||
objsym.refs:=objsym.refs or symref_from_text;
|
||||
end;
|
||||
end;
|
||||
|
||||
case reltyp of
|
||||
R_ARM_ABS32:
|
||||
begin
|
||||
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_ARM_GOT_ABS,
|
||||
//R_ARM_GOT_PREL,
|
||||
//R_ARM_GOT_BREL12,
|
||||
R_ARM_GOT_BREL:
|
||||
begin
|
||||
AllocGOTSlot(objreloc.symbol);
|
||||
end;
|
||||
|
||||
R_ARM_TLS_IE32:
|
||||
AllocGOTSlot(objreloc.symbol);
|
||||
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputARM.DoRelocationFixup(objsec:TObjSection);
|
||||
var
|
||||
i,zero:longint;
|
||||
objreloc: TObjRelocation;
|
||||
tmp,
|
||||
address,
|
||||
relocval : aint;
|
||||
relocsec : TObjSection;
|
||||
data: TDynamicArray;
|
||||
reltyp: byte;
|
||||
group:longint;
|
||||
rotation:longint;
|
||||
residual,g_n:longword;
|
||||
curloc: 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;
|
||||
|
||||
{ TODO: TARGET1 and TARGET2 are intended to be configured via commandline }
|
||||
if (reltyp=R_ARM_TARGET1) then
|
||||
reltyp:=R_ARM_ABS32; { may be ABS32 or REL32 }
|
||||
if (reltyp=R_ARM_TARGET2) then
|
||||
reltyp:=R_ARM_ABS32; { may be ABS32,REL32 or GOT_PREL }
|
||||
|
||||
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 if (reltyp=R_ARM_V4BX) then
|
||||
continue // ignore for now
|
||||
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;
|
||||
|
||||
curloc:=objsec.mempos+objreloc.dataoffset;
|
||||
if (relocsec=nil) or (relocsec.used) then
|
||||
case reltyp of
|
||||
|
||||
R_ARM_ABS32:
|
||||
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(objreloc.dataoffset+objsec.mempos,R_ARM_RELATIVE,0,address);
|
||||
end
|
||||
else
|
||||
{ Don't modify address in this case, as it serves as addend for RTLD }
|
||||
WriteDynRelocEntry(objreloc.dataoffset+objsec.mempos,R_ARM_ABS32,objreloc.symbol.exesymbol.dynindex,0);
|
||||
end
|
||||
else
|
||||
address:=address+relocval;
|
||||
end;
|
||||
|
||||
R_ARM_REL32:
|
||||
begin
|
||||
address:=address+relocval-curloc;
|
||||
end;
|
||||
|
||||
R_ARM_PC24,
|
||||
R_ARM_PLT32,
|
||||
R_ARM_JUMP24,
|
||||
R_ARM_CALL:
|
||||
begin
|
||||
{ R_ARM_PC24 is deprecated in favour of R_ARM_JUMP24 and R_ARM_CALL,
|
||||
which allow to distinguish opcodes without examining them.
|
||||
Difference is:
|
||||
1) when target is Thumb, BL can be changed to BLX, while B has
|
||||
to go via thunking code.
|
||||
2) when target is unresolved weak symbol, CALL must be changed to NOP,
|
||||
while JUMP24 behavior is unspecified. }
|
||||
tmp:=sarlongint((address and $00FFFFFF) shl 8,6);
|
||||
tmp:=tmp+relocval-curloc;
|
||||
// TODO: check overflow
|
||||
address:=(address and $FF000000) or ((tmp and $3FFFFFE) shr 2);
|
||||
end;
|
||||
|
||||
R_ARM_BASE_PREL: { GOTPC }
|
||||
address:=address+gotsymbol.address-curloc;
|
||||
|
||||
R_ARM_GOT_BREL: { GOT32 }
|
||||
begin
|
||||
MaybeWriteGOTEntry(reltyp,relocval,objreloc.symbol);
|
||||
address:=address+gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint)-gotsymbol.address;
|
||||
end;
|
||||
|
||||
R_ARM_GOTOFF32:
|
||||
address:=address+relocval-gotsymbol.address;
|
||||
|
||||
R_ARM_ALU_PC_G0_NC,
|
||||
R_ARM_ALU_PC_G1_NC,
|
||||
R_ARM_ALU_PC_G0,
|
||||
R_ARM_ALU_PC_G1,
|
||||
R_ARM_ALU_PC_G2,
|
||||
R_ARM_ALU_SB_G0_NC,
|
||||
R_ARM_ALU_SB_G1_NC,
|
||||
R_ARM_ALU_SB_G0,
|
||||
R_ARM_ALU_SB_G1,
|
||||
R_ARM_ALU_SB_G2:
|
||||
begin
|
||||
group:=(relocprops[reltyp].flags and gpmask)-1;
|
||||
if group<0 then
|
||||
InternalError(2012112601);
|
||||
|
||||
if (not ElfTarget.relocs_use_addend) then
|
||||
begin
|
||||
{ initial addend must be determined by parsing the instruction }
|
||||
tmp:=address and $FF;
|
||||
rotation:=(address and $F00) shr 7; { is in multpile of 2 bits }
|
||||
if rotation<>0 then
|
||||
tmp:=RorDword(tmp,rotation);
|
||||
case (address and $1E00000) of
|
||||
1 shl 23: ; { ADD instruction }
|
||||
1 shl 22: tmp:=-tmp; { SUB instruction }
|
||||
else
|
||||
Comment(v_error,'Group ALU relocations are permitted only for ADD or SUB instructions');
|
||||
continue;
|
||||
end;
|
||||
end
|
||||
else { TODO: must read the instruction anyway!! }
|
||||
tmp:=address;
|
||||
|
||||
if (relocprops[reltyp].flags and pc)<>0 then
|
||||
tmp:=tmp+relocval-curloc
|
||||
else
|
||||
tmp:=tmp+relocval{-SB}; { assuming zero segment base }
|
||||
|
||||
g_n:=group_reloc_mask(abs(tmp),group,residual);
|
||||
{TODO: check for overflow}
|
||||
|
||||
address:=address and $FF1FF000 or g_n;
|
||||
{ set opcode depending on the sign of resulting value }
|
||||
if tmp<0 then
|
||||
address:=address or (1 shl 22)
|
||||
else
|
||||
address:=address or (1 shl 23);
|
||||
end;
|
||||
|
||||
R_ARM_LDR_PC_G0,
|
||||
R_ARM_LDR_PC_G1,
|
||||
R_ARM_LDR_PC_G2,
|
||||
R_ARM_LDR_SB_G0,
|
||||
R_ARM_LDR_SB_G1,
|
||||
R_ARM_LDR_SB_G2:
|
||||
begin
|
||||
group:=(relocprops[reltyp].flags and gpmask)-1;
|
||||
if group<0 then
|
||||
InternalError(2012112602);
|
||||
|
||||
if (not ElfTarget.relocs_use_addend) then
|
||||
begin
|
||||
tmp:=(address and $FFF);
|
||||
if (address and (1 shl 23))=0 then
|
||||
tmp:=-tmp;
|
||||
end
|
||||
else { TODO: must read the instruction anyway }
|
||||
tmp:=address;
|
||||
|
||||
if (relocprops[reltyp].flags and pc)<>0 then
|
||||
tmp:=tmp+relocval-curloc
|
||||
else
|
||||
tmp:=tmp+relocval{-SB}; { assuming zero segment base }
|
||||
|
||||
group_reloc_mask(abs(tmp),group-1,residual);
|
||||
if residual>$FFF then
|
||||
InternalError(2012112603); { TODO: meaningful overflow error message }
|
||||
|
||||
address:=address and $FF7FF000 or residual;
|
||||
if tmp>=0 then
|
||||
address:=address or (1 shl 23);
|
||||
end;
|
||||
|
||||
R_ARM_LDRS_PC_G0,
|
||||
R_ARM_LDRS_PC_G1,
|
||||
R_ARM_LDRS_PC_G2,
|
||||
R_ARM_LDRS_SB_G0,
|
||||
R_ARM_LDRS_SB_G1,
|
||||
R_ARM_LDRS_SB_G2:
|
||||
begin
|
||||
group:=(relocprops[reltyp].flags and gpmask)-1;
|
||||
if group<0 then
|
||||
InternalError(2012112606);
|
||||
|
||||
if (not ElfTarget.relocs_use_addend) then
|
||||
begin
|
||||
tmp:=((address and $F00) shr 4) or (address and $F);
|
||||
if (address and (1 shl 23))=0 then
|
||||
tmp:=-tmp;
|
||||
end
|
||||
else { TODO: must read the instruction anyway }
|
||||
tmp:=address;
|
||||
|
||||
if (relocprops[reltyp].flags and pc)<>0 then
|
||||
tmp:=tmp+relocval-curloc
|
||||
else
|
||||
tmp:=tmp+relocval{-SB}; { assuming zero segment base }
|
||||
|
||||
group_reloc_mask(abs(tmp),group-1,residual);
|
||||
if (residual>$FF) then
|
||||
InternalError(2012112607); { TODO: meaningful overflow error message }
|
||||
|
||||
address:=address and $FF7FF0F0 or ((residual and $F0) shl 4) or (residual and $F);
|
||||
if tmp>=0 then
|
||||
address:=address or (1 shl 23);
|
||||
end;
|
||||
|
||||
R_ARM_LDC_PC_G0,
|
||||
R_ARM_LDC_PC_G1,
|
||||
R_ARM_LDC_PC_G2,
|
||||
R_ARM_LDC_SB_G0,
|
||||
R_ARM_LDC_SB_G1,
|
||||
R_ARM_LDC_SB_G2:
|
||||
begin
|
||||
group:=(relocprops[reltyp].flags and gpmask)-1;
|
||||
if group<0 then
|
||||
InternalError(2012112604);
|
||||
|
||||
if (not ElfTarget.relocs_use_addend) then
|
||||
begin
|
||||
tmp:=(address and $FF) shl 2;
|
||||
if (address and (1 shl 23))=0 then
|
||||
tmp:=-tmp;
|
||||
end
|
||||
else { TODO: must read the instruction anyway }
|
||||
tmp:=address;
|
||||
|
||||
if (relocprops[reltyp].flags and pc)<>0 then
|
||||
tmp:=tmp+relocval-curloc
|
||||
else
|
||||
tmp:=tmp+relocval{-SB}; { assuming zero segment base }
|
||||
|
||||
group_reloc_mask(abs(tmp),group-1,residual);
|
||||
{ residual must be divisible by 4 and fit into 8 bits after having been divided }
|
||||
if ((residual and 3)<>0) or (residual>$3FF) then
|
||||
InternalError(2012112605); { TODO: meaningful overflow error message }
|
||||
|
||||
address:=address and $FF7FFF00 or (residual shr 2);
|
||||
if tmp>=0 then
|
||||
address:=address or (1 shl 23);
|
||||
end;
|
||||
|
||||
R_ARM_TLS_IE32:
|
||||
begin
|
||||
relocval:=relocval-tlsseg.mempos+align_aword(TCB_SIZE,tlsseg.align);
|
||||
MaybeWriteGOTEntry(reltyp,relocval,objreloc.symbol);
|
||||
{ resolves to PC-relative offset to GOT slot }
|
||||
relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
|
||||
address:=address+relocval-curloc;
|
||||
end;
|
||||
|
||||
R_ARM_TLS_LE32:
|
||||
if IsSharedLibrary then
|
||||
{ TODO: error message saying "recompile with -Cg" isn't correct. Or is it? }
|
||||
ReportNonDSOReloc(reltyp,objsec,objreloc)
|
||||
else
|
||||
address:=relocval-tlsseg.mempos+align_aword(TCB_SIZE,tlsseg.align);
|
||||
|
||||
else
|
||||
begin
|
||||
writeln(objreloc.ftype);
|
||||
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_arm: TElfTarget =
|
||||
(
|
||||
max_page_size: $8000;
|
||||
exe_image_base: $8000;
|
||||
machine_code: EM_ARM;
|
||||
relocs_use_addend: false;
|
||||
dyn_reloc_codes: (
|
||||
R_ARM_RELATIVE,
|
||||
R_ARM_GLOB_DAT,
|
||||
R_ARM_JUMP_SLOT,
|
||||
R_ARM_COPY,
|
||||
R_ARM_IRELATIVE
|
||||
);
|
||||
relocname: @elf_arm_relocName;
|
||||
encodereloc: @elf_arm_encodeReloc;
|
||||
loadreloc: @elf_arm_loadReloc;
|
||||
loadsection: @elf_arm_loadSection;
|
||||
);
|
||||
|
||||
initialization
|
||||
ElfTarget:=elf_target_arm;
|
||||
ElfExeOutputClass:=TElfExeOutputARM;
|
||||
|
||||
end.
|
||||
|
@ -69,6 +69,8 @@ implementation
|
||||
{$endif}
|
||||
|
||||
,ogcoff
|
||||
,ogelf
|
||||
,cpuelf
|
||||
|
||||
{**************************************
|
||||
Assembler Readers
|
||||
|
574
compiler/mips/cpuelf.pas
Normal file
574
compiler/mips/cpuelf.pas
Normal file
@ -0,0 +1,574 @@
|
||||
{
|
||||
Copyright (c) 2012 by Sergei Gorelkin
|
||||
|
||||
Includes ELF-related code specific to MIPS
|
||||
|
||||
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;
|
||||
|
||||
interface
|
||||
|
||||
{$i fpcdefs.inc}
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
globtype,sysutils,cutils,cclasses,
|
||||
verbose, elfbase,
|
||||
systems,aasmbase,ogbase,ogelf,assemble;
|
||||
|
||||
type
|
||||
TElfExeOutputMIPS=class(TElfExeOutput)
|
||||
private
|
||||
gpdispsym: TObjSymbol;
|
||||
dt_gotsym_value: longint;
|
||||
dt_local_gotno_value: longint;
|
||||
procedure MaybeWriteGOTEntry(reltyp:byte;relocval:aint;objsym:TObjSymbol);
|
||||
protected
|
||||
procedure PrepareGOT;override;
|
||||
function AllocGOTSlot(objsym:TObjSymbol):boolean;override;
|
||||
procedure CreateGOTSection;override;
|
||||
procedure CreatePLT;override;
|
||||
procedure WriteTargetDynamicTags;override;
|
||||
// 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;
|
||||
public
|
||||
procedure DataPos_Start;override;
|
||||
end;
|
||||
|
||||
const
|
||||
{ section types }
|
||||
SHT_MIPS_LIBLIST = $70000000;
|
||||
SHT_MIPS_CONFLICT = $70000002;
|
||||
SHT_MIPS_GPTAB = $70000003;
|
||||
SHT_MIPS_UCODE = $70000004;
|
||||
SHT_MIPS_DEBUG = $70000005;
|
||||
SHT_MIPS_REGINFO = $70000006;
|
||||
|
||||
{ section flags }
|
||||
SHF_MIPS_GPREL = $10000000;
|
||||
|
||||
{ relocations }
|
||||
R_MIPS_NONE = 0;
|
||||
R_MIPS_16 = 1;
|
||||
R_MIPS_32 = 2;
|
||||
R_MIPS_REL32 = 3;
|
||||
R_MIPS_26 = 4;
|
||||
R_MIPS_HI16 = 5;
|
||||
R_MIPS_LO16 = 6;
|
||||
R_MIPS_GPREL16 = 7;
|
||||
R_MIPS_LITERAL = 8;
|
||||
R_MIPS_GOT16 = 9;
|
||||
R_MIPS_PC16 = 10;
|
||||
R_MIPS_CALL16 = 11;
|
||||
R_MIPS_GPREL32 = 12;
|
||||
R_MIPS_GOT_HI16 = 21;
|
||||
R_MIPS_GOT_LO16 = 22;
|
||||
R_MIPS_CALL_HI16 = 30;
|
||||
R_MIPS_CALL_LO16 = 31;
|
||||
|
||||
{ dynamic tags }
|
||||
DT_MIPS_RLD_VERSION = $70000001;
|
||||
DT_MIPS_TIME_STAMP = $70000002;
|
||||
DT_MIPS_ICHECKSUM = $70000003;
|
||||
DT_MIPS_IVERSION = $70000004;
|
||||
DT_MIPS_FLAGS = $70000005;
|
||||
DT_MIPS_BASE_ADDRESS = $70000006;
|
||||
DT_MIPS_CONFLICT = $70000008;
|
||||
DT_MIPS_LIBLIST = $70000009;
|
||||
DT_MIPS_LOCAL_GOTNO = $7000000A;
|
||||
DT_MIPS_CONFLICTNO = $7000000B;
|
||||
DT_MIPS_LIBLISTNO = $70000010;
|
||||
DT_MIPS_SYMTABNO = $70000011;
|
||||
DT_MIPS_UNREFEXTNO = $70000012;
|
||||
DT_MIPS_GOTSYM = $70000013;
|
||||
DT_MIPS_HIPAGENO = $70000014;
|
||||
DT_MIPS_RLD_MAP = $70000016;
|
||||
|
||||
{ values of DT_MIPS_FLAGS }
|
||||
RHF_QUICKSTART = 1;
|
||||
RHF_NOTPOT = 2;
|
||||
|
||||
|
||||
type
|
||||
TElfReginfo=record
|
||||
ri_gprmask: longword;
|
||||
ri_cprmask: array[0..3] of longword;
|
||||
ri_gp_value: longint; // signed
|
||||
end;
|
||||
|
||||
|
||||
procedure MaybeSwapElfReginfo(var h:TElfReginfo);
|
||||
var
|
||||
i: longint;
|
||||
begin
|
||||
if source_info.endian<>target_info.endian then
|
||||
begin
|
||||
h.ri_gprmask:=swapendian(h.ri_gprmask);
|
||||
for i:=0 to 3 do
|
||||
h.ri_cprmask[i]:=swapendian(h.ri_cprmask[i]);
|
||||
h.ri_gp_value:=swapendian(h.ri_gp_value);
|
||||
end;
|
||||
end;
|
||||
|
||||
{****************************************************************************
|
||||
ELF Target methods
|
||||
****************************************************************************}
|
||||
|
||||
function elf_mips_encodereloc(objrel:TObjRelocation):byte;
|
||||
begin
|
||||
case objrel.typ of
|
||||
RELOC_NONE:
|
||||
result:=R_MIPS_NONE;
|
||||
RELOC_ABSOLUTE:
|
||||
result:=R_MIPS_32;
|
||||
else
|
||||
result:=0;
|
||||
InternalError(2012110602);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
function elf_mips_relocname(reltyp:byte):string;
|
||||
begin
|
||||
result:='TODO';
|
||||
end;
|
||||
|
||||
|
||||
procedure elf_mips_loadreloc(objrel:TObjRelocation);
|
||||
begin
|
||||
end;
|
||||
|
||||
|
||||
function elf_mips_loadsection(objinput:TElfObjInput;objdata:TObjData;const shdr:TElfsechdr;shindex:longint):boolean;
|
||||
var
|
||||
secname:string;
|
||||
begin
|
||||
case shdr.sh_type of
|
||||
SHT_MIPS_REGINFO:
|
||||
result:=true;
|
||||
else
|
||||
writeln('elf_mips_loadsection: ',hexstr(shdr.sh_type,8),' ',objdata.name);
|
||||
result:=false;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
{*****************************************************************************
|
||||
TElfExeOutputMIPS
|
||||
*****************************************************************************}
|
||||
|
||||
procedure TElfExeOutputMIPS.CreateGOTSection;
|
||||
var
|
||||
tmp: longword;
|
||||
begin
|
||||
gotobjsec:=TElfObjSection.create_ext(internalObjData,'.got',
|
||||
SHT_PROGBITS,SHF_ALLOC or SHF_WRITE or SHF_MIPS_GPREL,sizeof(pint),sizeof(pint));
|
||||
gotobjsec.SecOptions:=[oso_keep];
|
||||
{ gotpltobjsec is what's pointed to by DT_PLTGOT }
|
||||
{ TODO: this is not correct; under some circumstances ld can generate PLTs for MIPS,
|
||||
using classic model. We'll need to support it, too. }
|
||||
gotpltobjsec:=TElfObjSection(gotobjsec);
|
||||
|
||||
internalObjData.SetSection(gotobjsec);
|
||||
{ TODO: must be an absolute symbol; binutils use linker script to define it }
|
||||
gotsymbol:=internalObjData.SymbolDefine('_gp',AB_GLOBAL,AT_NONE);
|
||||
gotsymbol.offset:=$7ff0;
|
||||
{ also define _gp_disp }
|
||||
gpdispsym:=internalObjData.SymbolDefine('_gp_disp',AB_GLOBAL,AT_NONE);
|
||||
{ reserved entries }
|
||||
gotobjsec.WriteZeros(sizeof(pint));
|
||||
tmp:=$80000000;
|
||||
if target_info.endian<>source_info.endian then
|
||||
tmp:=swapendian(tmp);
|
||||
gotobjsec.Write(tmp,sizeof(pint));
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputMIPS.CreatePLT;
|
||||
begin
|
||||
pltobjsec:=TElfObjSection.create_ext(internalObjData,'.plt',
|
||||
SHT_PROGBITS,SHF_ALLOC or SHF_EXECINSTR,4,16);
|
||||
pltobjsec.SecOptions:=[oso_keep];
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputMIPS.WriteTargetDynamicTags;
|
||||
begin
|
||||
writeDynTag(DT_MIPS_RLD_VERSION,1);
|
||||
if not IsSharedLibrary then
|
||||
{writeDynTag(DT_MIPS_RLD_MAP,rldmapsec)};
|
||||
writeDynTag(DT_MIPS_FLAGS,RHF_NOTPOT);
|
||||
if IsSharedLibrary then
|
||||
writeDynTag(DT_MIPS_BASE_ADDRESS,0)
|
||||
else
|
||||
writeDynTag(DT_MIPS_BASE_ADDRESS,ElfTarget.exe_image_base);
|
||||
writeDynTag(DT_MIPS_LOCAL_GOTNO,dt_local_gotno_value);
|
||||
writeDynTag(DT_MIPS_SYMTABNO,dynsymlist.count+1);
|
||||
{ ABI says: "Index of first external dynamic symbol not referenced locally" }
|
||||
{ What the hell is this? BFD writes number of output sections(!!),
|
||||
the values found in actual files do not match even that,
|
||||
and don't seem to be connected to reality at all... }
|
||||
//writeDynTag(DT_MIPS_UNREFEXTNO,0);
|
||||
{Index of first dynamic symbol in GOT }
|
||||
writeDynTag(DT_MIPS_GOTSYM,dt_gotsym_value+1);
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputMIPS.WritePLTEntry(exesym: TExeSymbol);
|
||||
begin
|
||||
|
||||
end;
|
||||
|
||||
|
||||
function TElfExeOutputMIPS.AllocGOTSlot(objsym:TObjSymbol):boolean;
|
||||
var
|
||||
exesym: TExeSymbol;
|
||||
begin
|
||||
{ MIPS has quite a different way of allocating GOT slots and dynamic relocations }
|
||||
result:=false;
|
||||
exesym:=objsym.exesymbol;
|
||||
|
||||
{ Although local symbols should not be accessed through GOT,
|
||||
this isn't strictly forbidden. In this case we need to fake up
|
||||
the exesym to store the GOT offset in it.
|
||||
TODO: name collision; maybe use a different symbol list object? }
|
||||
if exesym=nil then
|
||||
begin
|
||||
exesym:=TExeSymbol.Create(ExeSymbolList,objsym.name+'*local*');
|
||||
exesym.objsymbol:=objsym;
|
||||
objsym.exesymbol:=exesym;
|
||||
end;
|
||||
if exesym.GotOffset>0 then
|
||||
exit;
|
||||
make_dynamic_if_undefweak(exesym);
|
||||
|
||||
if (exesym.dynindex>0) and (exesym.ObjSymbol.ObjSection=nil) then
|
||||
begin
|
||||
{ External symbols must be located at the end of GOT, here just
|
||||
mark them for dealing later. }
|
||||
exesym.GotOffset:=high(aword);
|
||||
exit;
|
||||
end;
|
||||
gotobjsec.alloc(sizeof(pint));
|
||||
exesym.GotOffset:=gotobjsec.size;
|
||||
result:=true;
|
||||
end;
|
||||
|
||||
|
||||
function put_externals_last(p1,p2:pointer):longint;
|
||||
var
|
||||
sym1: TExeSymbol absolute p1;
|
||||
sym2: TExeSymbol absolute p2;
|
||||
begin
|
||||
result:=ord(sym1.gotoffset=high(aword))-ord(sym2.gotoffset=high(aword));
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputMIPS.PrepareGOT;
|
||||
var
|
||||
i: longint;
|
||||
exesym: TExeSymbol;
|
||||
begin
|
||||
inherited PrepareGOT;
|
||||
if not dynamiclink then
|
||||
exit;
|
||||
dynsymlist.sort(@put_externals_last);
|
||||
{ reindex, as sorting could changed the order }
|
||||
for i:=0 to dynsymlist.count-1 do
|
||||
TExeSymbol(dynsymlist[i]).dynindex:=i+1;
|
||||
{ find the symbol to be written as DT_GOTSYM }
|
||||
for i:=dynsymlist.count-1 downto 0 do
|
||||
begin
|
||||
exesym:=TExeSymbol(dynsymlist[i]);
|
||||
if exesym.gotoffset<>high(aword) then
|
||||
begin
|
||||
dt_gotsym_value:=i+1;
|
||||
break;
|
||||
end;
|
||||
end;
|
||||
{ !! maybe incorrect, where do 'unmapped globals' belong? }
|
||||
dt_local_gotno_value:=gotobjsec.size div sizeof(pint);
|
||||
|
||||
{ actually allocate GOT slots for imported symbols }
|
||||
for i:=dt_gotsym_value to dynsymlist.count-1 do
|
||||
begin
|
||||
exesym:=TExeSymbol(dynsymlist[i]);
|
||||
gotobjsec.alloc(sizeof(pint));
|
||||
exesym.GotOffset:=gotobjsec.size;
|
||||
end;
|
||||
gotsize:=gotobjsec.size;
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfExeOutputMIPS.DataPos_Start;
|
||||
begin
|
||||
{ Since we omit GOT slots for imported symbols during inherited PrepareGOT, they don't
|
||||
get written in ResolveRelocations either. This must be compensated here.
|
||||
Or better override ResolveRelocations and handle there. }
|
||||
{ TODO: shouldn't be zeroes, but address of stubs if address taken, etc. }
|
||||
gotobjsec.writeZeros(gotsize-gotobjsec.size);
|
||||
inherited DataPos_Start;
|
||||
end;
|
||||
|
||||
procedure TElfExeOutputMIPS.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 }
|
||||
if gotoff=gotobjsec.Data.size+sizeof(pint) then
|
||||
begin
|
||||
if source_info.endian<>target_info.endian then
|
||||
relocval:=swapendian(relocval);
|
||||
gotobjsec.write(relocval,sizeof(pint));
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TElfExeOutputMIPS.GOTRelocPass1(objsec:TObjSection;var idx:longint);
|
||||
var
|
||||
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_MIPS_CALL16,
|
||||
R_MIPS_GOT16:
|
||||
begin
|
||||
//TODO: GOT16 against local symbols need specialized handling
|
||||
AllocGOTSlot(objreloc.symbol);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
type
|
||||
PRelocData=^TRelocData;
|
||||
TRelocData=record
|
||||
next:PRelocData;
|
||||
objsec:TObjSection;
|
||||
objrel:TObjRelocation;
|
||||
addend:aint;
|
||||
end;
|
||||
|
||||
procedure TElfExeOutputMIPS.DoRelocationFixup(objsec:TObjSection);
|
||||
var
|
||||
i,zero:longint;
|
||||
objreloc: TObjRelocation;
|
||||
AHL_S,
|
||||
tmp,
|
||||
address,
|
||||
relocval : aint;
|
||||
relocsec : TObjSection;
|
||||
data: TDynamicArray;
|
||||
reltyp: byte;
|
||||
curloc: aword;
|
||||
reloclist,hr: PRelocData;
|
||||
is_gp_disp: boolean;
|
||||
begin
|
||||
data:=objsec.data;
|
||||
reloclist:=nil;
|
||||
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);
|
||||
if source_info.endian<>target_info.endian then
|
||||
address:=swapendian(address);
|
||||
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;
|
||||
|
||||
curloc:=objsec.mempos+objreloc.dataoffset;
|
||||
if (relocsec=nil) or (relocsec.used) then
|
||||
case reltyp of
|
||||
|
||||
R_MIPS_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
|
||||
end
|
||||
else
|
||||
;
|
||||
end
|
||||
else
|
||||
address:=address+relocval;
|
||||
end;
|
||||
|
||||
R_MIPS_26:
|
||||
begin
|
||||
tmp:=(address and $03FFFFFF) shl 2;
|
||||
tmp:=((tmp or (curloc and $F0000000))+relocval) shr 2;
|
||||
address:=(address and $FC000000) or (tmp and $3FFFFFF);
|
||||
end;
|
||||
|
||||
R_MIPS_HI16:
|
||||
begin
|
||||
{ This relocation can be handled only after seeing a matching LO16 one,
|
||||
moreover BFD supports any number of HI16 to precede a single LO16.
|
||||
So just add it to a queue. }
|
||||
new(hr);
|
||||
hr^.next:=reloclist;
|
||||
hr^.objrel:=objreloc;
|
||||
hr^.objsec:=objsec;
|
||||
hr^.addend:=address; //TODO: maybe it can be saved in objrel.orgsize field
|
||||
reloclist:=hr;
|
||||
end;
|
||||
|
||||
R_MIPS_LO16:
|
||||
begin
|
||||
while assigned(reloclist) do
|
||||
begin
|
||||
hr:=reloclist;
|
||||
reloclist:=hr^.next;
|
||||
// if relocval<>hr^.relocval then // must be the same symbol
|
||||
// InternalError();
|
||||
{ _gp_disp magic }
|
||||
is_gp_disp:=assigned(hr^.objrel.symbol) and
|
||||
assigned(hr^.objrel.symbol.exesymbol) and
|
||||
(hr^.objrel.symbol.exesymbol.objsymbol=gpdispsym);
|
||||
if is_gp_disp then
|
||||
relocval:=gotsymbol.address-curloc;
|
||||
AHL_S:=(hr^.addend shl 16)+SmallInt(address)+relocval;
|
||||
{ formula: ((AHL + S) – (short)(AHL + S)) >> 16 }
|
||||
tmp:=(hr^.addend and $FFFF0000) or ((AHL_S-SmallInt(AHL_S)) shr 16);
|
||||
data.seek(hr^.objrel.dataoffset);
|
||||
if source_info.endian<>target_info.endian then
|
||||
tmp:=swapendian(tmp);
|
||||
data.Write(tmp,4);
|
||||
dispose(hr);
|
||||
end;
|
||||
if is_gp_disp then
|
||||
Inc(AHL_S,4);
|
||||
address:=(address and $FFFF0000) or (AHL_S and $FFFF);
|
||||
end;
|
||||
|
||||
R_MIPS_CALL16,
|
||||
R_MIPS_GOT16:
|
||||
begin
|
||||
//TODO: GOT16 relocations against local symbols need specialized handling
|
||||
MaybeWriteGOTEntry(reltyp,relocval,objreloc.symbol);
|
||||
// !! this is correct only while _gp symbol is defined relative to .got !!
|
||||
relocval:=-(gotsymbol.offset-(objreloc.symbol.exesymbol.gotoffset-sizeof(pint)));
|
||||
// TODO: check overflow
|
||||
address:=(address and $FFFF0000) or (relocval and $FFFF);
|
||||
end;
|
||||
|
||||
R_MIPS_PC16:
|
||||
//TODO: check overflow
|
||||
address:=(address and $FFFF0000) or ((((SmallInt(address) shl 2)+relocval-curloc) shr 2) and $FFFF);
|
||||
|
||||
else
|
||||
begin
|
||||
writeln(objsec.fullname,'+',objreloc.dataoffset,' ',objreloc.ftype);
|
||||
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);
|
||||
if source_info.endian<>target_info.endian then
|
||||
address:=swapendian(address);
|
||||
data.Write(address,4);
|
||||
end;
|
||||
end;
|
||||
|
||||
{*****************************************************************************
|
||||
Initialize
|
||||
*****************************************************************************}
|
||||
|
||||
const
|
||||
elf_target_mips: TElfTarget =
|
||||
(
|
||||
max_page_size: $10000;
|
||||
exe_image_base: $400000;
|
||||
machine_code: EM_MIPS;
|
||||
relocs_use_addend: false;
|
||||
dyn_reloc_codes: (
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0
|
||||
);
|
||||
relocname: @elf_mips_relocName;
|
||||
encodereloc: @elf_mips_encodeReloc;
|
||||
loadreloc: @elf_mips_loadReloc;
|
||||
loadsection: @elf_mips_loadSection;
|
||||
);
|
||||
|
||||
initialization
|
||||
ElfTarget:=elf_target_mips;
|
||||
ElfExeOutputClass:=TElfExeOutputMIPS;
|
||||
|
||||
end.
|
||||
|
@ -52,6 +52,8 @@ implementation
|
||||
**************************************}
|
||||
|
||||
,CpuGas
|
||||
,ogelf
|
||||
,cpuelf
|
||||
|
||||
{**************************************
|
||||
Debuginfo
|
||||
|
Loading…
Reference in New Issue
Block a user