ogelf.pas 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary elf writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ogelf;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. cpuinfo,cpubase,aasmbase,aasmtai,aasmdata,assemble,
  27. { ELF definitions }
  28. elfbase,
  29. { output }
  30. ogbase,
  31. owbase;
  32. type
  33. {$ifdef cpu64bitaddr}
  34. TElfsechdr = TElf64sechdr;
  35. {$else cpu64bitaddr}
  36. TElfsechdr = TElf32sechdr;
  37. {$endif cpu64bitaddr}
  38. TElfObjSection = class(TObjSection)
  39. public
  40. shstridx,
  41. shtype,
  42. shflags,
  43. shlink,
  44. shinfo,
  45. shentsize : longint;
  46. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  47. constructor create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags:longint;Aalign:shortint;Aentsize:longint);
  48. constructor create_reloc(aobjdata:TObjData;const Aname:string;allocflag:boolean);
  49. procedure writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);override;
  50. end;
  51. TElfSymtabKind = (esk_obj,esk_exe,esk_dyn);
  52. TElfSymtab = class(TElfObjSection)
  53. public
  54. kind: TElfSymtabKind;
  55. fstrsec: TObjSection;
  56. symidx: longint;
  57. tlsbase: aword;
  58. constructor create(aObjData:TObjData;aKind:TElfSymtabKind);reintroduce;
  59. procedure writeSymbol(objsym:TObjSymbol;nameidx:longword=0);
  60. procedure writeInternalSymbol(avalue:aword;astridx:longword;ainfo:byte;ashndx:word);
  61. end;
  62. TElfObjData = class(TObjData)
  63. public
  64. constructor create(const n:string);override;
  65. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  66. procedure CreateDebugSections;override;
  67. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);override;
  68. end;
  69. TElfObjectOutput = class(tObjOutput)
  70. private
  71. symtabsect: TElfSymtab;
  72. shstrtabsect: TElfObjSection;
  73. procedure createrelocsection(s:TElfObjSection;data:TObjData);
  74. procedure createshstrtab(data:TObjData);
  75. procedure createsymtab(data: TObjData);
  76. procedure writesectionheader(s:TElfObjSection);
  77. procedure section_write_symbol(p:TObject;arg:pointer);
  78. procedure section_count_sections(p:TObject;arg:pointer);
  79. procedure section_create_relocsec(p:TObject;arg:pointer);
  80. procedure section_write_sechdr(p:TObject;arg:pointer);
  81. protected
  82. function writedata(data:TObjData):boolean;override;
  83. public
  84. constructor Create(AWriter:TObjectWriter);override;
  85. end;
  86. TElfAssembler = class(tinternalassembler)
  87. constructor create(smart:boolean);override;
  88. end;
  89. PSectionRec=^TSectionRec;
  90. TSectionRec=record
  91. sec: TObjSection;
  92. relocpos: aword;
  93. relocs: longint;
  94. relentsize: longint;
  95. end;
  96. TElfsecheaderarray=array of TElfsechdr;
  97. TElfObjInput=class(TObjInput)
  98. private
  99. FSecTbl: PSectionRec;
  100. FSymTbl: PPointer;
  101. FLoaded: PBoolean;
  102. shdrs: TElfsecheaderarray;
  103. nsects: longword;
  104. shentsize: longword;
  105. shoffset: aword;
  106. shstrndx: longword;
  107. symtabndx: longword;
  108. shstrtab: PChar;
  109. strtab: PChar;
  110. shstrtablen: longword;
  111. strtablen: longword;
  112. symtaboffset: aword;
  113. syms: longword;
  114. localsyms: longword;
  115. symversions: PWord;
  116. dynobj: boolean;
  117. function LoadHeader:word;
  118. procedure LoadSection(const shdr:TElfsechdr;index:longint;objdata:TObjData);
  119. procedure LoadRelocations(const secrec:TSectionRec);
  120. procedure LoadSymbols(objdata:TObjData;count,locals:longword);
  121. procedure LoadDynamic(const shdr:TElfsechdr;objdata:TObjData);
  122. public
  123. constructor Create;override;
  124. destructor Destroy;override;
  125. function ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;override;
  126. class function CanReadObjData(AReader:TObjectreader):boolean;override;
  127. function CreateSection(const shdr:TElfsechdr;index:longint;objdata:TObjData;
  128. out secname:string):TElfObjSection;
  129. end;
  130. TRelocNameProc=function(reltyp:byte):string;
  131. TEncodeRelocProc=function(objrel:TObjRelocation):byte;
  132. TLoadRelocProc=procedure(objrel:TObjRelocation);
  133. TLoadSectionProc=function(objinput:TElfObjInput;objdata:TObjData;const shdr:TElfsechdr;shindex:longint):boolean;
  134. TDynamicReloc=(
  135. dr_relative,
  136. dr_glob_dat,
  137. dr_jump_slot,
  138. dr_copy,
  139. dr_irelative
  140. );
  141. TElfTarget=record
  142. max_page_size: longword;
  143. exe_image_base: longword;
  144. machine_code: word;
  145. relocs_use_addend: boolean;
  146. dyn_reloc_codes: array[TDynamicReloc] of byte;
  147. relocname: TRelocNameProc;
  148. encodereloc: TEncodeRelocProc;
  149. loadreloc: TLoadRelocProc;
  150. loadsection: TLoadSectionProc;
  151. end;
  152. TElfExeSection=class(TExeSection)
  153. public
  154. secshidx : longword; { index of the section header }
  155. shstridx,
  156. shtype,
  157. shflags,
  158. shlink,
  159. shinfo,
  160. shentsize : longword;
  161. procedure AddObjSection(objsec:TObjSection;ignoreprops:boolean=false);override;
  162. end;
  163. TElfSegment=class
  164. public
  165. ptype: longword;
  166. pflags: longword;
  167. DataPos: aword;
  168. DataSize: aword;
  169. MemPos: aword;
  170. MemSize: aword;
  171. align: longword;
  172. //physaddr: aword;
  173. FSectionList: TFPObjectList;
  174. constructor Create(atype,aflags,aalign:longword);
  175. destructor Destroy; override;
  176. procedure Add(exesec:TExeSection);
  177. end;
  178. TElfExeOutput=class(TExeOutput)
  179. private
  180. segmentlist: TFPObjectList;
  181. textseg,
  182. dataseg,
  183. noteseg,
  184. phdrseg: TElfSegment;
  185. shstrtabsect: TElfObjSection;
  186. symtab: TElfSymtab;
  187. shoffset: aword;
  188. gotwritten: boolean;
  189. { dynamic linking }
  190. dynamiclink: boolean;
  191. dynsymnames: Plongword;
  192. dynsymtable: TElfSymtab;
  193. interpobjsec: TObjSection;
  194. FInterpreter: pshortstring;
  195. verneedcount,
  196. verdefcount: longword;
  197. symversec,
  198. verdefsec,
  199. verneedsec,
  200. dynamicsec,
  201. hashobjsec: TElfObjSection;
  202. neededlist: TFPHashList;
  203. gotsize: aword;
  204. dynrelsize: aword;
  205. function AttachSection(objsec:TObjSection):TElfExeSection;
  206. function CreateSegment(atype,aflags,aalign:longword):TElfSegment;
  207. procedure CreatePLT;
  208. procedure WriteHeader;
  209. procedure WriteDynamicSymbolsHash;
  210. procedure WriteDynamicTags;
  211. procedure FinishDynamicTags;
  212. procedure exesection_write_header(p:TObject;arg:Pointer);
  213. procedure segment_write_header(p:TObject;arg:Pointer);
  214. procedure mempos_segment(seg:TElfSegment);
  215. procedure datapos_segment(seg:TElfSegment);
  216. procedure MapSectionsToSegments;
  217. procedure WriteStaticSymtable;
  218. procedure InitDynlink;
  219. procedure PrepareGOT;
  220. procedure WriteDynTag(aTag:longword;aValue:longword);
  221. procedure WriteDynTag(aTag:longword;aSection:TObjSection;aOffs:aword=0);
  222. protected
  223. hastextrelocs: boolean;
  224. gotsymbol: TObjSymbol;
  225. dynsymlist: TFPObjectList;
  226. gotobjsec: TObjSection;
  227. pltobjsec,
  228. gotpltobjsec,
  229. pltrelocsec,
  230. ipltrelocsec,
  231. dynrelocsec: TElfObjSection;
  232. dynreloclist: TFPObjectList;
  233. tlsseg: TElfSegment;
  234. relative_reloc_count: longint;
  235. function AllocGOTSlot(objsym: TObjSymbol):boolean;
  236. procedure WriteDynRelocEntry(dataofs:aword;typ:byte;symidx:aword;addend:aword);
  237. procedure WriteFirstPLTEntry;virtual;abstract;
  238. procedure WritePLTEntry(exesym:TExeSymbol);virtual;
  239. procedure WriteIndirectPLTEntry(exesym:TExeSymbol);virtual;
  240. procedure GOTRelocPass1(objsec:TObjSection;var idx:longint);virtual;abstract;
  241. procedure ReportNonDSOReloc(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  242. procedure ReportRelocOverflow(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  243. public
  244. constructor Create;override;
  245. destructor Destroy;override;
  246. procedure Load_Start;override;
  247. procedure Load_DynamicObject(ObjData:TObjData;asneeded:boolean);override;
  248. procedure Order_Start;override;
  249. procedure Order_end;override;
  250. procedure AfterUnusedSectionRemoval;override;
  251. procedure MemPos_Start;override;
  252. procedure MemPos_ExeSection(const aname:string);override;
  253. procedure DataPos_Start;override;
  254. procedure DataPos_ExeSection(const aname:string);override;
  255. function writeData:boolean;override;
  256. procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override;
  257. property interpreter:pshortstring read FInterpreter write FInterpreter;
  258. end;
  259. var
  260. ElfExeOutputClass: TExeOutputClass;
  261. ElfTarget: TElfTarget;
  262. const
  263. { Bits of TObjSymbol.refs field }
  264. symref_plt = 1;
  265. implementation
  266. uses
  267. SysUtils,
  268. verbose,
  269. export,expunix,
  270. cutils,globals,fmodule;
  271. const
  272. symbolresize = 200*18;
  273. {$ifdef cpu64bitaddr}
  274. const
  275. ELFCLASS = ELFCLASS64;
  276. type
  277. telfheader = telf64header;
  278. telfreloc = telf64reloc;
  279. telfsymbol = telf64symbol;
  280. telfproghdr = telf64proghdr;
  281. telfdyn = telf64dyn;
  282. function ELF_R_INFO(sym:longword;typ:byte):qword;inline;
  283. begin
  284. result:=(qword(sym) shl 32) or typ;
  285. end;
  286. {$else cpu64bitaddr}
  287. const
  288. ELFCLASS = ELFCLASS32;
  289. type
  290. telfheader = telf32header;
  291. telfreloc = telf32reloc;
  292. telfsymbol = telf32symbol;
  293. telfproghdr = telf32proghdr;
  294. telfdyn = telf32dyn;
  295. function ELF_R_INFO(sym:longword;typ:byte):longword;inline;
  296. begin
  297. result:=(sym shl 8) or typ;
  298. end;
  299. {$endif cpu64bitaddr}
  300. procedure MayBeSwapHeader(var h : telf32header);
  301. begin
  302. if source_info.endian<>target_info.endian then
  303. with h do
  304. begin
  305. e_type:=swapendian(e_type);
  306. e_machine:=swapendian(e_machine);
  307. e_version:=swapendian(e_version);
  308. e_entry:=swapendian(e_entry);
  309. e_phoff:=swapendian(e_phoff);
  310. e_shoff:=swapendian(e_shoff);
  311. e_flags:=swapendian(e_flags);
  312. e_ehsize:=swapendian(e_ehsize);
  313. e_phentsize:=swapendian(e_phentsize);
  314. e_phnum:=swapendian(e_phnum);
  315. e_shentsize:=swapendian(e_shentsize);
  316. e_shnum:=swapendian(e_shnum);
  317. e_shstrndx:=swapendian(e_shstrndx);
  318. end;
  319. end;
  320. procedure MayBeSwapHeader(var h : telf64header);
  321. begin
  322. if source_info.endian<>target_info.endian then
  323. with h do
  324. begin
  325. e_type:=swapendian(e_type);
  326. e_machine:=swapendian(e_machine);
  327. e_version:=swapendian(e_version);
  328. e_entry:=swapendian(e_entry);
  329. e_phoff:=swapendian(e_phoff);
  330. e_shoff:=swapendian(e_shoff);
  331. e_flags:=swapendian(e_flags);
  332. e_ehsize:=swapendian(e_ehsize);
  333. e_phentsize:=swapendian(e_phentsize);
  334. e_phnum:=swapendian(e_phnum);
  335. e_shentsize:=swapendian(e_shentsize);
  336. e_shnum:=swapendian(e_shnum);
  337. e_shstrndx:=swapendian(e_shstrndx);
  338. end;
  339. end;
  340. procedure MayBeSwapHeader(var h : telf32proghdr);
  341. begin
  342. if source_info.endian<>target_info.endian then
  343. with h do
  344. begin
  345. p_align:=swapendian(p_align);
  346. p_filesz:=swapendian(p_filesz);
  347. p_flags:=swapendian(p_flags);
  348. p_memsz:=swapendian(p_memsz);
  349. p_offset:=swapendian(p_offset);
  350. p_paddr:=swapendian(p_paddr);
  351. p_type:=swapendian(p_type);
  352. p_vaddr:=swapendian(p_vaddr);
  353. end;
  354. end;
  355. procedure MayBeSwapHeader(var h : telf64proghdr);
  356. begin
  357. if source_info.endian<>target_info.endian then
  358. with h do
  359. begin
  360. p_align:=swapendian(p_align);
  361. p_filesz:=swapendian(p_filesz);
  362. p_flags:=swapendian(p_flags);
  363. p_memsz:=swapendian(p_memsz);
  364. p_offset:=swapendian(p_offset);
  365. p_paddr:=swapendian(p_paddr);
  366. p_type:=swapendian(p_type);
  367. p_vaddr:=swapendian(p_vaddr);
  368. end;
  369. end;
  370. procedure MaybeSwapSecHeader(var h : telf32sechdr);
  371. begin
  372. if source_info.endian<>target_info.endian then
  373. with h do
  374. begin
  375. sh_name:=swapendian(sh_name);
  376. sh_type:=swapendian(sh_type);
  377. sh_flags:=swapendian(sh_flags);
  378. sh_addr:=swapendian(sh_addr);
  379. sh_offset:=swapendian(sh_offset);
  380. sh_size:=swapendian(sh_size);
  381. sh_link:=swapendian(sh_link);
  382. sh_info:=swapendian(sh_info);
  383. sh_addralign:=swapendian(sh_addralign);
  384. sh_entsize:=swapendian(sh_entsize);
  385. end;
  386. end;
  387. procedure MaybeSwapSecHeader(var h : telf64sechdr);
  388. begin
  389. if source_info.endian<>target_info.endian then
  390. with h do
  391. begin
  392. sh_name:=swapendian(sh_name);
  393. sh_type:=swapendian(sh_type);
  394. sh_flags:=swapendian(sh_flags);
  395. sh_addr:=swapendian(sh_addr);
  396. sh_offset:=swapendian(sh_offset);
  397. sh_size:=swapendian(sh_size);
  398. sh_link:=swapendian(sh_link);
  399. sh_info:=swapendian(sh_info);
  400. sh_addralign:=swapendian(sh_addralign);
  401. sh_entsize:=swapendian(sh_entsize);
  402. end;
  403. end;
  404. procedure MaybeSwapElfSymbol(var h : telf32symbol);
  405. begin
  406. if source_info.endian<>target_info.endian then
  407. with h do
  408. begin
  409. st_name:=swapendian(st_name);
  410. st_value:=swapendian(st_value);
  411. st_size:=swapendian(st_size);
  412. st_shndx:=swapendian(st_shndx);
  413. end;
  414. end;
  415. procedure MaybeSwapElfSymbol(var h : telf64symbol);
  416. begin
  417. if source_info.endian<>target_info.endian then
  418. with h do
  419. begin
  420. st_name:=swapendian(st_name);
  421. st_value:=swapendian(st_value);
  422. st_size:=swapendian(st_size);
  423. st_shndx:=swapendian(st_shndx);
  424. end;
  425. end;
  426. procedure MaybeSwapElfReloc(var h : telf32reloc);
  427. begin
  428. if source_info.endian<>target_info.endian then
  429. with h do
  430. begin
  431. address:=swapendian(address);
  432. info:=swapendian(info);
  433. addend:=swapendian(addend);
  434. end;
  435. end;
  436. procedure MaybeSwapElfReloc(var h : telf64reloc);
  437. begin
  438. if source_info.endian<>target_info.endian then
  439. with h do
  440. begin
  441. address:=swapendian(address);
  442. info:=swapendian(info);
  443. addend:=swapendian(addend);
  444. end;
  445. end;
  446. procedure MaybeSwapElfDyn(var h : telf32dyn);
  447. begin
  448. if source_info.endian<>target_info.endian then
  449. with h do
  450. begin
  451. d_tag:=swapendian(d_tag);
  452. d_val:=swapendian(d_val);
  453. end;
  454. end;
  455. procedure MaybeSwapElfDyn(var h : telf64dyn);
  456. begin
  457. if source_info.endian<>target_info.endian then
  458. with h do
  459. begin
  460. d_tag:=swapendian(d_tag);
  461. d_val:=swapendian(d_val);
  462. end;
  463. end;
  464. procedure MaybeSwapElfverdef(var h: TElfverdef);
  465. begin
  466. if source_info.endian<>target_info.endian then
  467. with h do
  468. begin
  469. vd_version:=swapendian(vd_version);
  470. vd_flags:=swapendian(vd_flags);
  471. vd_ndx:=swapendian(vd_ndx);
  472. vd_cnt:=swapendian(vd_cnt);
  473. vd_hash:=swapendian(vd_hash);
  474. vd_aux:=swapendian(vd_aux);
  475. vd_next:=swapendian(vd_next);
  476. end;
  477. end;
  478. procedure MaybeSwapElfverdaux(var h: TElfverdaux);
  479. begin
  480. if source_info.endian<>target_info.endian then
  481. with h do
  482. begin
  483. vda_name:=swapendian(vda_name);
  484. vda_next:=swapendian(vda_next);
  485. end;
  486. end;
  487. procedure MaybeSwapElfverneed(var h: TElfverneed);
  488. begin
  489. if source_info.endian<>target_info.endian then
  490. with h do
  491. begin
  492. vn_version:=swapendian(vn_version);
  493. vn_cnt:=swapendian(vn_cnt);
  494. vn_file:=swapendian(vn_file);
  495. vn_aux:=swapendian(vn_aux);
  496. vn_next:=swapendian(vn_next);
  497. end;
  498. end;
  499. procedure MaybeSwapElfvernaux(var h: TElfvernaux);
  500. begin
  501. if source_info.endian<>target_info.endian then
  502. with h do
  503. begin
  504. vna_hash:=swapendian(vna_hash);
  505. vna_flags:=swapendian(vna_flags);
  506. vna_other:=swapendian(vna_other);
  507. vna_name:=swapendian(vna_name);
  508. vna_next:=swapendian(vna_next);
  509. end;
  510. end;
  511. {****************************************************************************
  512. Helpers
  513. ****************************************************************************}
  514. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  515. begin
  516. { Section Type }
  517. AshType:=SHT_PROGBITS;
  518. if oso_strings in aoptions then
  519. AshType:=SHT_STRTAB
  520. else if not(oso_data in aoptions) then
  521. AshType:=SHT_NOBITS;
  522. { Section Flags }
  523. Ashflags:=0;
  524. if oso_load in aoptions then
  525. Ashflags:=Ashflags or SHF_ALLOC;
  526. if oso_executable in aoptions then
  527. Ashflags:=Ashflags or SHF_EXECINSTR;
  528. if oso_write in aoptions then
  529. Ashflags:=Ashflags or SHF_WRITE;
  530. end;
  531. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  532. begin
  533. aoptions:=[];
  534. { Section Type }
  535. if AshType<>SHT_NOBITS then
  536. include(aoptions,oso_data);
  537. if AshType=SHT_STRTAB then
  538. include(aoptions,oso_strings);
  539. { Section Flags }
  540. if Ashflags and SHF_ALLOC<>0 then
  541. include(aoptions,oso_load);
  542. if Ashflags and SHF_WRITE<>0 then
  543. include(aoptions,oso_write);
  544. if Ashflags and SHF_EXECINSTR<>0 then
  545. include(aoptions,oso_executable);
  546. end;
  547. {****************************************************************************
  548. TElfObjSection
  549. ****************************************************************************}
  550. constructor TElfObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  551. begin
  552. inherited create(AList,Aname,Aalign,aoptions);
  553. index:=0;
  554. shstridx:=0;
  555. encodesechdrflags(aoptions,shtype,shflags);
  556. shlink:=0;
  557. shinfo:=0;
  558. if name='.stab' then
  559. shentsize:=sizeof(TObjStabEntry);
  560. end;
  561. constructor TElfObjSection.create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags:longint;Aalign:shortint;Aentsize:longint);
  562. var
  563. aoptions : TObjSectionOptions;
  564. begin
  565. decodesechdrflags(Ashtype,Ashflags,aoptions);
  566. inherited create(aobjdata.ObjSectionList,Aname,Aalign,aoptions);
  567. objdata:=aobjdata;
  568. index:=0;
  569. shstridx:=0;
  570. shtype:=AshType;
  571. shflags:=AshFlags;
  572. shentsize:=Aentsize;
  573. end;
  574. const
  575. relsec_prefix:array[boolean] of string[5] = ('.rel','.rela');
  576. relsec_shtype:array[boolean] of longword = (SHT_REL,SHT_RELA);
  577. constructor TElfObjSection.create_reloc(aobjdata:TObjData;const Aname:string;allocflag:boolean);
  578. begin
  579. create_ext(aobjdata,
  580. relsec_prefix[ElfTarget.relocs_use_addend]+aname,
  581. relsec_shtype[ElfTarget.relocs_use_addend],
  582. SHF_ALLOC*ord(allocflag),
  583. sizeof(pint),
  584. (2+ord(ElfTarget.relocs_use_addend))*sizeof(pint));
  585. end;
  586. procedure TElfObjSection.writeReloc_internal(aTarget:TObjSection;offset:aword;len:byte;reltype:TObjRelocationType);
  587. var
  588. reloc: TObjRelocation;
  589. begin
  590. reloc:=TObjRelocation.CreateSection(Size,aTarget,reltype);
  591. reloc.size:=len;
  592. ObjRelocations.Add(reloc);
  593. if reltype=RELOC_RELATIVE then
  594. dec(offset,len)
  595. else if reltype<>RELOC_ABSOLUTE then
  596. InternalError(2012062401);
  597. if ElfTarget.relocs_use_addend then
  598. begin
  599. reloc.orgsize:=offset;
  600. offset:=0;
  601. end;
  602. write(offset,len);
  603. end;
  604. {****************************************************************************
  605. TElfObjData
  606. ****************************************************************************}
  607. constructor TElfObjData.create(const n:string);
  608. begin
  609. inherited create(n);
  610. CObjSection:=TElfObjSection;
  611. end;
  612. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  613. const
  614. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  615. { TODO: sec_rodata is still writable }
  616. '.text','.data','.data','.rodata','.bss','.threadvar',
  617. '.pdata',
  618. '.text', { darwin stubs }
  619. '__DATA,__nl_symbol_ptr',
  620. '__DATA,__la_symbol_ptr',
  621. '__DATA,__mod_init_func',
  622. '__DATA,__mod_term_func',
  623. '.stab','.stabstr',
  624. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  625. '.eh_frame',
  626. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  627. '.fpc',
  628. '.toc',
  629. '.init',
  630. '.fini',
  631. '.objc_class',
  632. '.objc_meta_class',
  633. '.objc_cat_cls_meth',
  634. '.objc_cat_inst_meth',
  635. '.objc_protocol',
  636. '.objc_string_object',
  637. '.objc_cls_meth',
  638. '.objc_inst_meth',
  639. '.objc_cls_refs',
  640. '.objc_message_refs',
  641. '.objc_symbols',
  642. '.objc_category',
  643. '.objc_class_vars',
  644. '.objc_instance_vars',
  645. '.objc_module_info',
  646. '.objc_class_names',
  647. '.objc_meth_var_types',
  648. '.objc_meth_var_names',
  649. '.objc_selector_strs',
  650. '.objc_protocol_ext',
  651. '.objc_class_ext',
  652. '.objc_property',
  653. '.objc_image_info',
  654. '.objc_cstring_object',
  655. '.objc_sel_fixup',
  656. '__DATA,__objc_data',
  657. '__DATA,__objc_const',
  658. '.objc_superrefs',
  659. '__DATA, __datacoal_nt,coalesced',
  660. '.objc_classlist',
  661. '.objc_nlclasslist',
  662. '.objc_catlist',
  663. '.obcj_nlcatlist',
  664. '.objc_protolist'
  665. );
  666. var
  667. sep : string[3];
  668. secname : string;
  669. begin
  670. { section type user gives the user full controll on the section name }
  671. if atype=sec_user then
  672. result:=aname
  673. else
  674. begin
  675. secname:=secnames[atype];
  676. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  677. begin
  678. result:=secname+'.'+aname;
  679. exit;
  680. end;
  681. if create_smartlink_sections and (aname<>'') then
  682. begin
  683. case aorder of
  684. secorder_begin :
  685. sep:='.b_';
  686. secorder_end :
  687. sep:='.z_';
  688. else
  689. sep:='.n_';
  690. end;
  691. result:=secname+sep+aname
  692. end
  693. else
  694. result:=secname;
  695. end;
  696. end;
  697. procedure TElfObjData.CreateDebugSections;
  698. begin
  699. if target_dbg.id=dbg_stabs then
  700. begin
  701. stabssec:=createsection(sec_stab);
  702. stabstrsec:=createsection(sec_stabstr);
  703. end;
  704. end;
  705. procedure TElfObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);
  706. var
  707. symaddr : aint;
  708. objreloc: TObjRelocation;
  709. begin
  710. if CurrObjSec=nil then
  711. internalerror(200403292);
  712. objreloc:=nil;
  713. if assigned(p) then
  714. begin
  715. { real address of the symbol }
  716. symaddr:=p.address;
  717. { Local ObjSymbols can be resolved already or need a section reloc }
  718. if (p.bind=AB_LOCAL) and
  719. (reltype in [RELOC_RELATIVE,RELOC_ABSOLUTE{$ifdef x86_64},RELOC_ABSOLUTE32{$endif x86_64}]) then
  720. begin
  721. { For a reltype relocation in the same section the
  722. value can be calculated }
  723. if (p.objsection=CurrObjSec) and
  724. (reltype=RELOC_RELATIVE) then
  725. inc(data,symaddr-len-CurrObjSec.Size)
  726. else
  727. begin
  728. objreloc:=TObjRelocation.CreateSection(CurrObjSec.Size,p.objsection,reltype);
  729. CurrObjSec.ObjRelocations.Add(objreloc);
  730. inc(data,symaddr);
  731. end;
  732. end
  733. else
  734. begin
  735. objreloc:=TObjRelocation.CreateSymbol(CurrObjSec.Size,p,reltype);
  736. CurrObjSec.ObjRelocations.Add(objreloc);
  737. { If target is a local label and it isn't handled above,
  738. patch its type in order to get it written to symtable.
  739. This may happen e.g. when taking address of Pascal label in PIC mode. }
  740. if (p.bind=AB_LOCAL) and (p.typ=AT_LABEL) then
  741. p.typ:=AT_ADDR;
  742. end;
  743. end;
  744. if assigned(objreloc) then
  745. begin
  746. objreloc.size:=len;
  747. if reltype in [RELOC_RELATIVE{$ifdef x86},RELOC_PLT32{$endif}{$ifdef x86_64},RELOC_GOTPCREL{$endif}] then
  748. dec(data,len);
  749. if ElfTarget.relocs_use_addend then
  750. begin
  751. objreloc.orgsize:=data;
  752. data:=0;
  753. end;
  754. end;
  755. CurrObjSec.write(data,len);
  756. end;
  757. {****************************************************************************
  758. TElfSymtab
  759. ****************************************************************************}
  760. const
  761. symsecnames: array[boolean] of string[8] = ('.symtab','.dynsym');
  762. strsecnames: array[boolean] of string[8] = ('.strtab','.dynstr');
  763. symsectypes: array[boolean] of longword = (SHT_SYMTAB,SHT_DYNSYM);
  764. symsecattrs: array[boolean] of longword = (0,SHF_ALLOC);
  765. constructor TElfSymtab.create(aObjData:TObjData;aKind:TElfSymtabKind);
  766. var
  767. dyn:boolean;
  768. begin
  769. dyn:=(aKind=esk_dyn);
  770. create_ext(aObjData,symsecnames[dyn],symsectypes[dyn],symsecattrs[dyn],sizeof(pint),sizeof(TElfSymbol));
  771. fstrsec:=TElfObjSection.create_ext(aObjData,strsecnames[dyn],SHT_STRTAB,symsecattrs[dyn],1,0);
  772. fstrsec.writezeros(1);
  773. writezeros(sizeof(TElfSymbol));
  774. symidx:=1;
  775. shinfo:=1;
  776. kind:=aKind;
  777. end;
  778. procedure TElfSymtab.writeInternalSymbol(avalue:aword;astridx:longword;ainfo:byte;ashndx:word);
  779. var
  780. elfsym:TElfSymbol;
  781. begin
  782. fillchar(elfsym,sizeof(elfsym),0);
  783. elfsym.st_value:=avalue;
  784. elfsym.st_name:=astridx;
  785. elfsym.st_info:=ainfo;
  786. elfsym.st_shndx:=ashndx;
  787. inc(symidx);
  788. inc(shinfo);
  789. MaybeSwapElfSymbol(elfsym);
  790. write(elfsym,sizeof(elfsym));
  791. end;
  792. procedure TElfSymtab.writeSymbol(objsym:TObjSymbol;nameidx:longword);
  793. var
  794. elfsym:TElfSymbol;
  795. begin
  796. fillchar(elfsym,sizeof(elfsym),0);
  797. if nameidx=0 then
  798. elfsym.st_name:=fstrsec.writestr(objsym.name)
  799. else
  800. elfsym.st_name:=nameidx;
  801. elfsym.st_size:=objsym.size;
  802. elfsym.st_value:=objsym.address;
  803. case objsym.bind of
  804. AB_LOCAL :
  805. begin
  806. elfsym.st_info:=STB_LOCAL shl 4;
  807. inc(shinfo);
  808. end;
  809. AB_COMMON :
  810. begin
  811. elfsym.st_value:=var_align(objsym.size);
  812. elfsym.st_info:=STB_GLOBAL shl 4;
  813. elfsym.st_shndx:=SHN_COMMON;
  814. end;
  815. AB_EXTERNAL :
  816. elfsym.st_info:=STB_GLOBAL shl 4;
  817. AB_WEAK_EXTERNAL :
  818. elfsym.st_info:=STB_WEAK shl 4;
  819. AB_GLOBAL :
  820. elfsym.st_info:=STB_GLOBAL shl 4;
  821. else
  822. InternalError(2012111801);
  823. end;
  824. if (objsym.bind<>AB_EXTERNAL) then
  825. begin
  826. case objsym.typ of
  827. AT_FUNCTION :
  828. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  829. AT_DATA :
  830. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  831. AT_TLS:
  832. elfsym.st_info:=elfsym.st_info or STT_TLS;
  833. AT_GNU_IFUNC:
  834. elfsym.st_info:=elfsym.st_info or STT_GNU_IFUNC;
  835. { other types are implicitly mapped to STT_NOTYPE }
  836. end;
  837. end;
  838. if objsym.bind<>AB_COMMON then
  839. begin
  840. if kind<>esk_obj then
  841. begin
  842. if assigned(objsym.objsection) and assigned(objsym.objsection.ExeSection) then
  843. begin
  844. if (objsym.typ=AT_TLS) then
  845. elfsym.st_value:=elfsym.st_value-tlsbase
  846. else if (oso_plt in objsym.objsection.SecOptions) then
  847. elfsym.st_value:=0
  848. else
  849. elfsym.st_shndx:=TElfExeSection(objsym.objsection.ExeSection).secshidx;
  850. end;
  851. end
  852. else
  853. begin
  854. if assigned(objsym.objsection) then
  855. elfsym.st_shndx:=objsym.objsection.index
  856. else
  857. elfsym.st_shndx:=SHN_UNDEF;
  858. objsym.symidx:=symidx;
  859. end;
  860. end;
  861. inc(symidx);
  862. MaybeSwapElfSymbol(elfsym);
  863. write(elfsym,sizeof(TElfSymbol));
  864. end;
  865. {****************************************************************************
  866. TElfObjectOutput
  867. ****************************************************************************}
  868. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  869. begin
  870. inherited Create(AWriter);
  871. CObjData:=TElfObjData;
  872. end;
  873. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection;data:TObjData);
  874. var
  875. i : longint;
  876. rel : telfreloc;
  877. objreloc : TObjRelocation;
  878. relsym : longint;
  879. relocsect : TElfObjSection;
  880. begin
  881. { create the reloc section }
  882. relocsect:=TElfObjSection.create_reloc(data,s.name,false);
  883. relocsect.shlink:=symtabsect.index;
  884. relocsect.shinfo:=s.index;
  885. { add the relocations }
  886. for i:=0 to s.Objrelocations.count-1 do
  887. begin
  888. objreloc:=TObjRelocation(s.Objrelocations[i]);
  889. { Symbol }
  890. if assigned(objreloc.symbol) then
  891. begin
  892. if objreloc.symbol.symidx=-1 then
  893. begin
  894. writeln(objreloc.symbol.Name);
  895. internalerror(200603012);
  896. end;
  897. relsym:=objreloc.symbol.symidx;
  898. end
  899. else
  900. begin
  901. if objreloc.objsection<>nil then
  902. relsym:=objreloc.objsection.secsymidx
  903. else
  904. relsym:=SHN_UNDEF;
  905. end;
  906. rel.address:=objreloc.dataoffset;
  907. rel.info:=ELF_R_INFO(relsym,ElfTarget.encodereloc(objreloc));
  908. rel.addend:=objreloc.orgsize;
  909. { write reloc }
  910. { ElfXX_Rel is essentially ElfXX_Rela without the addend field. }
  911. MaybeSwapElfReloc(rel);
  912. relocsect.write(rel,relocsect.shentsize);
  913. end;
  914. end;
  915. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  916. begin
  917. { Must not write symbols for internal sections like .symtab }
  918. { TODO: maybe use inclusive list of section types instead }
  919. if (TElfObjSection(p).shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  920. exit;
  921. TObjSection(p).secsymidx:=symtabsect.symidx;
  922. symtabsect.writeInternalSymbol(0,0,STT_SECTION,TObjSection(p).index);
  923. end;
  924. procedure TElfObjectOutput.createsymtab(data: TObjData);
  925. var
  926. i : longint;
  927. objsym : TObjSymbol;
  928. begin
  929. with data do
  930. begin
  931. { section symbols }
  932. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  933. { First the Local Symbols, this is required by ELF. The localsyms
  934. count stored in shinfo is used to skip the local symbols
  935. when traversing the symtab }
  936. for i:=0 to ObjSymbolList.Count-1 do
  937. begin
  938. objsym:=TObjSymbol(ObjSymbolList[i]);
  939. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  940. symtabsect.WriteSymbol(objsym);
  941. end;
  942. { Global Symbols }
  943. for i:=0 to ObjSymbolList.Count-1 do
  944. begin
  945. objsym:=TObjSymbol(ObjSymbolList[i]);
  946. if (objsym.bind<>AB_LOCAL) then
  947. symtabsect.WriteSymbol(objsym);
  948. end;
  949. { update the .symtab section header }
  950. symtabsect.shlink:=symtabsect.fstrsec.index;
  951. end;
  952. end;
  953. procedure TElfObjectOutput.createshstrtab(data: TObjData);
  954. var
  955. i,prefixlen:longint;
  956. objsec,target:TElfObjSection;
  957. begin
  958. shstrtabsect.writezeros(1);
  959. prefixlen:=length('.rel')+ord(ElfTarget.relocs_use_addend);
  960. for i:=0 to data.ObjSectionList.Count-1 do
  961. begin
  962. objsec:=TElfObjSection(data.ObjSectionList[i]);
  963. { Alias section names into names of corresponding reloc sections,
  964. this is allowed by ELF specs and saves good half of .shstrtab space. }
  965. if objsec.shtype=relsec_shtype[ElfTarget.relocs_use_addend] then
  966. begin
  967. target:=TElfObjSection(data.ObjSectionList[objsec.shinfo-1]);
  968. if (target.ObjRelocations.Count=0) or
  969. (target.shstridx<prefixlen) then
  970. InternalError(2012101204);
  971. objsec.shstridx:=target.shstridx-prefixlen;
  972. end
  973. else
  974. begin
  975. if objsec.ObjRelocations.Count<>0 then
  976. shstrtabsect.write(relsec_prefix[true][1],prefixlen);
  977. objsec.shstridx:=shstrtabsect.writestr(objsec.name);
  978. end;
  979. end;
  980. end;
  981. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  982. var
  983. sechdr : telfsechdr;
  984. begin
  985. fillchar(sechdr,sizeof(sechdr),0);
  986. sechdr.sh_name:=s.shstridx;
  987. sechdr.sh_type:=s.shtype;
  988. sechdr.sh_flags:=s.shflags;
  989. sechdr.sh_offset:=s.datapos;
  990. sechdr.sh_size:=s.Size;
  991. sechdr.sh_link:=s.shlink;
  992. sechdr.sh_info:=s.shinfo;
  993. sechdr.sh_addralign:=s.secalign;
  994. sechdr.sh_entsize:=s.shentsize;
  995. MaybeSwapSecHeader(sechdr);
  996. writer.write(sechdr,sizeof(sechdr));
  997. end;
  998. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  999. begin
  1000. TElfObjSection(p).index:=pword(arg)^;
  1001. inc(pword(arg)^);
  1002. end;
  1003. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  1004. begin
  1005. if (TElfObjSection(p).ObjRelocations.count>0) then
  1006. createrelocsection(TElfObjSection(p),TObjData(arg));
  1007. end;
  1008. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  1009. begin
  1010. writesectionheader(TElfObjSection(p));
  1011. end;
  1012. function TElfObjectOutput.writedata(data:TObjData):boolean;
  1013. var
  1014. header : telfheader;
  1015. shoffset,
  1016. datapos : aword;
  1017. nsections : word;
  1018. begin
  1019. result:=false;
  1020. with data do
  1021. begin
  1022. { default sections }
  1023. symtabsect:=TElfSymtab.create(data,esk_obj);
  1024. shstrtabsect:=TElfObjSection.create_ext(data,'.shstrtab',SHT_STRTAB,0,1,0);
  1025. { "no executable stack" marker for Linux }
  1026. if (target_info.system in systems_linux) and
  1027. not(cs_executable_stack in current_settings.moduleswitches) then
  1028. TElfObjSection.create_ext(data,'.note.GNU-stack',SHT_PROGBITS,0,1,0);
  1029. { symbol for filename }
  1030. symtabsect.fstrsec.writestr(ExtractFileName(current_module.mainsource));
  1031. symtabsect.writeInternalSymbol(0,1,STT_FILE,SHN_ABS);
  1032. { calc amount of sections we have }
  1033. nsections:=1;
  1034. { also create the index in the section header table }
  1035. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1036. { create .symtab and .strtab }
  1037. createsymtab(data);
  1038. { Create the relocation sections, this needs valid secidx and symidx }
  1039. ObjSectionList.ForEachCall(@section_create_relocsec,data);
  1040. { recalc nsections to incude the reloc sections }
  1041. nsections:=1;
  1042. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1043. { create .shstrtab }
  1044. createshstrtab(data);
  1045. { Calculate the filepositions }
  1046. datapos:=$40; { elfheader + alignment }
  1047. { section data }
  1048. layoutsections(datapos);
  1049. { section headers }
  1050. shoffset:=datapos;
  1051. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  1052. { Write ELF Header }
  1053. fillchar(header,sizeof(header),0);
  1054. header.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  1055. header.e_ident[EI_MAG1]:=ELFMAG1;
  1056. header.e_ident[EI_MAG2]:=ELFMAG2;
  1057. header.e_ident[EI_MAG3]:=ELFMAG3;
  1058. header.e_ident[EI_CLASS]:=ELFCLASS;
  1059. if target_info.endian=endian_big then
  1060. header.e_ident[EI_DATA]:=ELFDATA2MSB
  1061. else
  1062. header.e_ident[EI_DATA]:=ELFDATA2LSB;
  1063. header.e_ident[EI_VERSION]:=1;
  1064. header.e_type:=ET_REL;
  1065. header.e_machine:=ElfTarget.machine_code;
  1066. header.e_version:=1;
  1067. header.e_shoff:=shoffset;
  1068. header.e_shstrndx:=shstrtabsect.index;
  1069. header.e_shnum:=nsections;
  1070. header.e_ehsize:=sizeof(telfheader);
  1071. header.e_shentsize:=sizeof(telfsechdr);
  1072. MaybeSwapHeader(header);
  1073. writer.write(header,sizeof(header));
  1074. writer.writezeros($40-sizeof(header)); { align }
  1075. { Sections }
  1076. WriteSectionContent(data);
  1077. { section headers, start with an empty header for sh_undef }
  1078. writer.writezeros(sizeof(telfsechdr));
  1079. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1080. end;
  1081. result:=true;
  1082. end;
  1083. {****************************************************************************
  1084. TELFAssembler
  1085. ****************************************************************************}
  1086. constructor TElfAssembler.Create(smart:boolean);
  1087. begin
  1088. inherited Create(smart);
  1089. CObjOutput:=TElfObjectOutput;
  1090. end;
  1091. {****************************************************************************
  1092. TELFObjectInput
  1093. ****************************************************************************}
  1094. constructor TElfObjInput.Create;
  1095. begin
  1096. inherited Create;
  1097. CObjData:=TElfObjData;
  1098. end;
  1099. destructor TElfObjInput.Destroy;
  1100. begin
  1101. if Assigned(FSymTbl) then
  1102. FreeMem(FSymTbl);
  1103. if Assigned(FSecTbl) then
  1104. FreeMem(FSecTbl);
  1105. if Assigned(strtab) then
  1106. FreeMem(strtab);
  1107. if Assigned(shstrtab) then
  1108. FreeMem(shstrtab);
  1109. if Assigned(symversions) then
  1110. FreeMem(symversions);
  1111. inherited Destroy;
  1112. end;
  1113. procedure TElfObjInput.LoadRelocations(const secrec:TSectionRec);
  1114. var
  1115. i: longint;
  1116. rel: TElfReloc;
  1117. reltyp: byte;
  1118. relsym: longint;
  1119. objrel: TObjRelocation;
  1120. p: TObjSymbol;
  1121. begin
  1122. FReader.Seek(secrec.relocpos);
  1123. if secrec.sec=nil then
  1124. InternalError(2012060203);
  1125. for i:=0 to secrec.relocs-1 do
  1126. begin
  1127. FReader.Read(rel,secrec.relentsize);
  1128. MaybeSwapElfReloc(rel);
  1129. reltyp:=rel.info and $FF;
  1130. {$ifdef cpu64bitaddr}
  1131. relsym:=rel.info shr 32;
  1132. {$else cpu64bitaddr}
  1133. relsym:=(rel.info shr 8) and $FFFFFF;
  1134. {$endif cpu64bitaddr}
  1135. if relsym>=syms then
  1136. InternalError(2012060204);
  1137. p:=TObjSymbol(FSymTbl[relsym]);
  1138. { Some relocations (e.g. R_ARM_V4BX) don't use a symbol at all }
  1139. if assigned(p) or (relsym=0) then
  1140. begin
  1141. objrel:=TObjRelocation.CreateRaw(rel.address-secrec.sec.mempos,p,reltyp);
  1142. if (secrec.relentsize=3*sizeof(pint)) then
  1143. objrel.orgsize:=rel.addend;
  1144. { perform target-specific actions }
  1145. if Assigned(ElfTarget.loadreloc) then
  1146. ElfTarget.loadreloc(objrel);
  1147. secrec.sec.ObjRelocations.add(objrel);
  1148. end
  1149. else
  1150. begin
  1151. InputError('Unable to resolve symbol of relocation');
  1152. exit;
  1153. end;
  1154. end;
  1155. end;
  1156. procedure TElfObjInput.LoadSymbols(objdata:TObjData;count,locals:longword);
  1157. var
  1158. i: longint;
  1159. sym: TElfSymbol;
  1160. bind: TAsmSymBind;
  1161. typ: TAsmSymType;
  1162. objsym: TObjSymbol;
  1163. ver: word;
  1164. begin
  1165. FSymTbl:=AllocMem(count*sizeof(Pointer));
  1166. for i:=1 to count-1 do
  1167. begin
  1168. FReader.Read(sym,sizeof(TElfSymbol));
  1169. MaybeSwapElfSymbol(sym);
  1170. if sym.st_name>=strtablen then
  1171. InternalError(2012060205);
  1172. if sym.st_shndx=SHN_ABS then { ignore absolute symbols (should we really do it???) }
  1173. Continue
  1174. else if sym.st_shndx=SHN_COMMON then
  1175. bind:=AB_COMMON
  1176. else if (sym.st_shndx>=nsects) then
  1177. InternalError(2012060206)
  1178. else
  1179. case (sym.st_info shr 4) of
  1180. STB_LOCAL:
  1181. bind:=AB_LOCAL;
  1182. STB_GLOBAL:
  1183. if sym.st_shndx=SHN_UNDEF then
  1184. bind:=AB_EXTERNAL
  1185. else
  1186. bind:=AB_GLOBAL;
  1187. STB_WEAK:
  1188. bind:=AB_WEAK_EXTERNAL;
  1189. else
  1190. InternalError(2012060207);
  1191. end;
  1192. { Ignore section symbol if we didn't create the corresponding objsection
  1193. (examples are SHT_GROUP or .note.GNU-stack sections). }
  1194. if (sym.st_shndx>0) and (sym.st_shndx<SHN_LORESERVE) and
  1195. (FSecTbl[sym.st_shndx].sec=nil) and
  1196. (not dynobj) then
  1197. if ((sym.st_info and $0F)=STT_SECTION) then
  1198. Continue
  1199. else
  1200. begin
  1201. writeln(objdata.name,' ',i);
  1202. InternalError(2012110701)
  1203. end;
  1204. case (sym.st_info and $0F) of
  1205. STT_NOTYPE:
  1206. typ:=AT_NONE;
  1207. STT_OBJECT:
  1208. typ:=AT_DATA;
  1209. STT_FUNC:
  1210. typ:=AT_FUNCTION;
  1211. STT_SECTION:
  1212. typ:=AT_SECTION;
  1213. STT_FILE:
  1214. continue;
  1215. STT_TLS:
  1216. typ:=AT_TLS;
  1217. STT_GNU_IFUNC:
  1218. typ:=AT_GNU_IFUNC;
  1219. else
  1220. writeln(objdata.name,' ',sym.st_info and $0F);
  1221. InternalError(2012060208);
  1222. end;
  1223. { If reading DSO, we're interested only in global symbols defined there.
  1224. Symbols with non-current version should also be ignored. }
  1225. if dynobj then
  1226. begin
  1227. if assigned(symversions) then
  1228. begin
  1229. ver:=symversions[i];
  1230. if (ver=0) or (ver > $7FFF) then
  1231. continue;
  1232. end;
  1233. if (bind= AB_LOCAL) or (sym.st_shndx=SHN_UNDEF) then
  1234. continue;
  1235. end;
  1236. { validity of name and objsection has been checked above }
  1237. { !! all AT_SECTION symbols have duplicate (null) name,
  1238. therefore TObjSection.CreateSymbol cannot be used here }
  1239. objsym:=TObjSymbol.Create(objdata.ObjSymbolList,string(PChar(@strtab[sym.st_name])));
  1240. objsym.bind:=bind;
  1241. objsym.typ:=typ;
  1242. if bind<>AB_COMMON then
  1243. objsym.objsection:=FSecTbl[sym.st_shndx].sec;
  1244. objsym.offset:=sym.st_value;
  1245. objsym.size:=sym.st_size;
  1246. FSymTbl[i]:=objsym;
  1247. end;
  1248. end;
  1249. function TElfObjInput.CreateSection(const shdr:TElfsechdr;index:longint;objdata:tobjdata;
  1250. out secname:string):TElfObjSection;
  1251. begin
  1252. secname:=string(PChar(@shstrtab[shdr.sh_name]));
  1253. result:=TElfObjSection.create_ext(objdata,secname,
  1254. shdr.sh_type,shdr.sh_flags,shdr.sh_addralign,shdr.sh_entsize);
  1255. result.index:=index;
  1256. result.DataPos:=shdr.sh_offset;
  1257. result.MemPos:=shdr.sh_addr;
  1258. result.Size:=shdr.sh_size;
  1259. FSecTbl[index].sec:=result;
  1260. end;
  1261. procedure TElfObjInput.LoadSection(const shdr:TElfsechdr;index:longint;objdata:tobjdata);
  1262. var
  1263. sec: TElfObjSection;
  1264. sym: TElfSymbol;
  1265. secname: string;
  1266. begin
  1267. if shdr.sh_name>=shstrtablen then
  1268. InternalError(2012060210);
  1269. case shdr.sh_type of
  1270. SHT_NULL:
  1271. {ignore};
  1272. { SHT_STRTAB may appear for .stabstr and other debug sections.
  1273. .shstrtab and .strtab are processed separately and don't appear here. }
  1274. SHT_PROGBITS,SHT_NOBITS,SHT_NOTE,SHT_STRTAB,
  1275. SHT_INIT_ARRAY,SHT_FINI_ARRAY,SHT_PREINIT_ARRAY:
  1276. begin
  1277. sec:=CreateSection(shdr,index,objdata,secname);
  1278. if (Length(secname)>3) and (secname[2] in ['d','f','n','s']) then
  1279. begin
  1280. if (Pos('.stub',secname)=1) or
  1281. (Pos('.fpc',secname)=1) then
  1282. sec.SecOptions:=[oso_keep]
  1283. { ELF does not have any flags specific to debug sections,
  1284. but reserves names starting with '.debug' for this purpose }
  1285. else if (Pos('.debug',secname)=1) or
  1286. (secname='.stab') or
  1287. (secname='.stabstr') then
  1288. sec.SecOptions:=[oso_debug]
  1289. else if (secname='.note.GNU-stack') and (shdr.sh_type=SHT_PROGBITS) then
  1290. begin
  1291. if (shdr.sh_flags and SHF_EXECINSTR)=0 then
  1292. objdata.ExecStack:=False;
  1293. end;
  1294. end;
  1295. if (shdr.sh_type=SHT_NOTE) and (shdr.sh_size<>0) then
  1296. sec.SecOptions:=[oso_keep];
  1297. end;
  1298. SHT_REL,SHT_RELA:
  1299. begin
  1300. if shdr.sh_info>=nsects then
  1301. InternalError(2012060211);
  1302. if shdr.sh_entsize<>longword((2+ord(shdr.sh_type=SHT_RELA))*sizeof(pint)) then
  1303. InternalError(2012060212);
  1304. with FSecTbl[shdr.sh_info] do
  1305. begin
  1306. relocpos:=shdr.sh_offset;
  1307. relocs:=shdr.sh_size div shdr.sh_entsize;
  1308. relentsize:=shdr.sh_entsize;
  1309. end;
  1310. end;
  1311. SHT_GROUP:
  1312. if (shdr.sh_size>=2*sizeof(longword)) and
  1313. (shdr.sh_entsize=sizeof(longword)) and
  1314. ((shdr.sh_size mod shdr.sh_entsize)=0) then
  1315. begin
  1316. { Groups are identified by name of symbol pointed to by
  1317. sh_link and sh_info, not by sh_name. This symbol
  1318. may as well be STT_SECTION symbol of this section,
  1319. in which case we end up using sh_name. }
  1320. if dynobj then
  1321. InternalError(2012110801);
  1322. if (shdr.sh_link<>symtabndx) then
  1323. InternalError(2012110703);
  1324. if (shdr.sh_info>=syms) then
  1325. InternalError(2012110704);
  1326. FReader.Seek(symtaboffset+shdr.sh_info*sizeof(TElfSymbol));
  1327. FReader.Read(sym,sizeof(TElfSymbol));
  1328. MaybeSwapElfSymbol(sym);
  1329. if sym.st_name>=strtablen then
  1330. InternalError(2012110705);
  1331. if (sym.st_shndx=index) and (sym.st_info=((STB_LOCAL shl 4) or STT_SECTION)) then
  1332. secname:=string(PChar(@shstrtab[shdr.sh_name]))
  1333. else
  1334. secname:=string(PChar(@strtab[sym.st_name]));
  1335. { Postpone further processing until all sections are loaded,
  1336. we'll need to access correct section header.
  1337. Since ABI requires SHT_GROUP sections to come first in the file,
  1338. we assume that group number x has header index x+1.
  1339. If we ever encounter files where this is not true, we'll have
  1340. to maintain a separate index. }
  1341. objdata.CreateSectionGroup(secname);
  1342. if (index<>objdata.GroupsList.Count) then
  1343. InternalError(2012110802);
  1344. end
  1345. else
  1346. InternalError(2012110706);
  1347. else
  1348. if not (assigned(ElfTarget.loadsection) and
  1349. ElfTarget.loadsection(self,objdata,shdr,index)) then
  1350. InternalError(2012072603);
  1351. end;
  1352. FLoaded[index]:=True;
  1353. end;
  1354. function TElfObjInput.LoadHeader:word;
  1355. var
  1356. header:TElfHeader;
  1357. begin
  1358. result:=ET_NONE;
  1359. if not FReader.read(header,sizeof(header)) then
  1360. begin
  1361. InputError('Can''t read ELF header');
  1362. exit;
  1363. end;
  1364. if (header.e_ident[EI_MAG0]<>ELFMAG0) or (header.e_ident[EI_MAG1]<>ELFMAG1) or
  1365. (header.e_ident[EI_MAG2]<>ELFMAG2) or (header.e_ident[EI_MAG3]<>ELFMAG3) then
  1366. begin
  1367. InputError('Illegal ELF magic');
  1368. exit;
  1369. end;
  1370. if (header.e_ident[EI_VERSION]<>1) then
  1371. begin
  1372. InputError('Unknown ELF file version');
  1373. exit;
  1374. end;
  1375. if (header.e_ident[EI_CLASS]<>ELFCLASS) then
  1376. begin
  1377. InputError('Wrong ELF file class (32/64 bit mismatch)');
  1378. exit;
  1379. end;
  1380. if (header.e_ident[EI_DATA]<>1+ord(target_info.endian=endian_big)) then
  1381. begin
  1382. InputError('ELF endianness does not match target');
  1383. exit;
  1384. end;
  1385. MaybeSwapHeader(header);
  1386. if (header.e_version<>1) then
  1387. begin
  1388. InputError('Unknown ELF data version');
  1389. exit;
  1390. end;
  1391. if (header.e_machine<>ElfTarget.machine_code) then
  1392. begin
  1393. InputError('ELF file is for different CPU');
  1394. exit;
  1395. end;
  1396. nsects:=header.e_shnum;
  1397. shentsize:=header.e_shentsize;
  1398. shoffset:=header.e_shoff;
  1399. shstrndx:=header.e_shstrndx;
  1400. result:=header.e_type;
  1401. end;
  1402. procedure TElfObjInput.LoadDynamic(const shdr:TElfsechdr;objdata:TObjData);
  1403. var
  1404. dt: TElfDyn;
  1405. i: longint;
  1406. begin
  1407. if (shdr.sh_entsize<>sizeof(TElfDyn)) then
  1408. InternalError(2012071403);
  1409. FReader.Seek(shdr.sh_offset);
  1410. for i:=0 to (shdr.sh_size div shdr.sh_entsize)-1 do
  1411. begin
  1412. FReader.Read(dt,sizeof(TElfDyn));
  1413. MaybeSwapElfDyn(dt);
  1414. case dt.d_tag of
  1415. DT_NULL:
  1416. break;
  1417. DT_SONAME:
  1418. TElfObjData(objdata).FName:=string(PChar(@strtab[dt.d_ptr]));
  1419. DT_NEEDED:
  1420. ;
  1421. end;
  1422. end;
  1423. end;
  1424. function TElfObjInput.ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;
  1425. var
  1426. i,j,strndx,dynndx,
  1427. versymndx,verdefndx,verneedndx: longint;
  1428. objsec: TObjSection;
  1429. grp: TObjSectionGroup;
  1430. tmp: longword;
  1431. count: longint;
  1432. begin
  1433. FReader:=AReader;
  1434. InputFileName:=AReader.FileName;
  1435. result:=false;
  1436. i:=LoadHeader;
  1437. if (i=ET_NONE) then { error message already given in this case }
  1438. exit;
  1439. if (i<>ET_REL) and (i<>ET_DYN) then
  1440. begin
  1441. InputError('Not a relocatable or dynamic ELF file');
  1442. exit;
  1443. end;
  1444. dynobj:=(i=ET_DYN);
  1445. if shentsize<>sizeof(TElfsechdr) then
  1446. InternalError(2012062701);
  1447. objdata:=CObjData.Create(InputFilename);
  1448. FSecTbl:=AllocMem(nsects*sizeof(TSectionRec));
  1449. FLoaded:=AllocMem(nsects*sizeof(boolean));
  1450. SetLength(shdrs,nsects);
  1451. FReader.Seek(shoffset);
  1452. if not FReader.Read(shdrs[0],nsects*sizeof(TElfsechdr)) then
  1453. begin
  1454. InputError('Can''t read ELF section headers');
  1455. exit;
  1456. end;
  1457. if source_info.endian<>target_info.endian then
  1458. for i:=0 to nsects-1 do
  1459. MaybeSwapSecHeader(shdrs[i]);
  1460. { First, load the .shstrtab section }
  1461. if shstrndx>=nsects then
  1462. InternalError(2012060201);
  1463. if shdrs[shstrndx].sh_type<>SHT_STRTAB then
  1464. InternalError(2012060202);
  1465. shstrtablen:=shdrs[shstrndx].sh_size;
  1466. GetMem(shstrtab,shstrtablen);
  1467. FReader.seek(shdrs[shstrndx].sh_offset);
  1468. FReader.read(shstrtab^,shstrtablen);
  1469. FLoaded[shstrndx]:=True;
  1470. { Locate the symtable, it is typically at the end so loop backwards.
  1471. Load the strings, postpone symtable itself until done with sections.
  1472. Note that is is legal to have no symtable.
  1473. For DSO, locate .dynsym instead, this one is near the beginning, but
  1474. overall number of sections won't be big. }
  1475. symtabndx:=0;
  1476. for i:=nsects-1 downto 1 do
  1477. begin
  1478. if (shdrs[i].sh_type<>symsectypes[dynobj]) then
  1479. continue;
  1480. if (shdrs[i].sh_entsize<>sizeof(TElfSymbol)) then
  1481. InternalError(2012060213);
  1482. if shdrs[i].sh_link>=nsects then
  1483. InternalError(2012062702);
  1484. strndx:=shdrs[i].sh_link;
  1485. if shdrs[strndx].sh_type<>SHT_STRTAB then
  1486. InternalError(2012062703);
  1487. strtablen:=shdrs[strndx].sh_size;
  1488. GetMem(strtab,strtablen);
  1489. FReader.seek(shdrs[strndx].sh_offset);
  1490. FReader.read(strtab^,strtablen);
  1491. symtaboffset:=shdrs[i].sh_offset;
  1492. syms:=shdrs[i].sh_size div sizeof(TElfSymbol);
  1493. localsyms:=shdrs[i].sh_info;
  1494. FLoaded[i]:=True;
  1495. FLoaded[strndx]:=True;
  1496. symtabndx:=i;
  1497. break;
  1498. end;
  1499. if dynobj then
  1500. begin
  1501. if symtabndx=0 then
  1502. InternalError(2012110707);
  1503. { Locate .dynamic and version sections. Expect a single one of a kind. }
  1504. dynndx:=0;
  1505. versymndx:=0;
  1506. verdefndx:=0;
  1507. verneedndx:=0;
  1508. for i:=nsects-1 downto 0 do
  1509. begin
  1510. case shdrs[i].sh_type of
  1511. SHT_DYNAMIC:
  1512. begin
  1513. if dynndx<>0 then
  1514. InternalError(2012102001);
  1515. dynndx:=i;
  1516. if (shdrs[dynndx].sh_link<>strndx) then
  1517. InternalError(2012071402);
  1518. LoadDynamic(shdrs[dynndx],objdata);
  1519. end;
  1520. SHT_GNU_versym:
  1521. begin
  1522. if versymndx<>0 then
  1523. InternalError(2012102002);
  1524. versymndx:=i;
  1525. if shdrs[i].sh_entsize<>sizeof(word) then
  1526. InternalError(2012102003);
  1527. if shdrs[i].sh_link<>symtabndx then
  1528. InternalError(2012102004);
  1529. if shdrs[i].sh_size<>syms*sizeof(word) then
  1530. InternalError(2012102005);
  1531. GetMem(symversions,shdrs[i].sh_size);
  1532. FReader.seek(shdrs[i].sh_offset);
  1533. FReader.read(symversions^,shdrs[i].sh_size);
  1534. if source_info.endian<>target_info.endian then
  1535. for j:=0 to syms-1 do
  1536. SwapEndian(symversions[j]);
  1537. end;
  1538. SHT_GNU_verdef:
  1539. begin
  1540. if verdefndx<>0 then
  1541. InternalError(2012102006);
  1542. verdefndx:=i;
  1543. //sh_link->.dynstr
  1544. //sh_info->.hash
  1545. end;
  1546. SHT_GNU_verneed:
  1547. begin
  1548. if verneedndx<>0 then
  1549. InternalError(2012102007);
  1550. verneedndx:=i;
  1551. //sh_link->.dynstr
  1552. //sh_info->hash
  1553. end;
  1554. end;
  1555. end;
  1556. if dynndx=0 then
  1557. InternalError(2012071401);
  1558. { for DSO, we aren't interested in actual sections, but need to a dummy one
  1559. to maintain integrity. }
  1560. objsec:=TElfObjSection.create_ext(objdata,'*DSO*',SHT_PROGBITS,0,0,0);
  1561. for i:=1 to nsects-1 do
  1562. FSecTbl[i].sec:=objsec;
  1563. { load the symtable }
  1564. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1565. LoadSymbols(objdata,syms,localsyms);
  1566. result:=True;
  1567. exit;
  1568. end;
  1569. { assume stack is executable until proven otherwise }
  1570. objdata.ExecStack:=True;
  1571. { Process section headers }
  1572. for i:=1 to nsects-1 do
  1573. if not FLoaded[i] then
  1574. LoadSection(shdrs[i],i,objdata);
  1575. { load the content }
  1576. ReadSectionContent(objdata);
  1577. { load the symtable }
  1578. FReader.Seek(symtaboffset+sizeof(TElfSymbol));
  1579. LoadSymbols(objdata,syms,localsyms);
  1580. { finish relocations }
  1581. for i:=0 to objdata.ObjSectionList.Count-1 do
  1582. begin
  1583. objsec:=TObjSection(objdata.ObjsectionList[i]);
  1584. { skip debug sections }
  1585. if (oso_debug in objsec.SecOptions) and
  1586. (cs_link_strip in current_settings.globalswitches) and
  1587. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1588. continue;
  1589. if FSecTbl[objsec.index].relocpos>0 then
  1590. LoadRelocations(FSecTbl[objsec.index]);
  1591. end;
  1592. { finish processing section groups, if any }
  1593. if Assigned(objdata.GroupsList) then
  1594. begin
  1595. for i:=0 to objdata.GroupsList.Count-1 do
  1596. begin
  1597. grp:=TObjSectionGroup(objData.GroupsList[i]);
  1598. FReader.Seek(shdrs[i+1].sh_offset);
  1599. { first dword is flags }
  1600. FReader.Read(tmp,sizeof(longword));
  1601. if source_info.endian<>target_info.endian then
  1602. tmp:=SwapEndian(tmp);
  1603. if (tmp and GRP_COMDAT)<>0 then
  1604. grp.IsComdat:=true;
  1605. count:=(shdrs[i+1].sh_size div sizeof(longword))-1;
  1606. SetLength(grp.members,count);
  1607. for j:=0 to count-1 do
  1608. begin
  1609. FReader.Read(tmp,sizeof(longword));
  1610. if source_info.endian<>target_info.endian then
  1611. tmp:=SwapEndian(tmp);
  1612. if (tmp>=nsects) then
  1613. InternalError(2012110805);
  1614. objsec:=FSecTbl[tmp].sec;
  1615. if (objsec=nil) then
  1616. InternalError(2012110806);
  1617. if (TElfObjSection(objsec).shflags and SHF_GROUP)=0 then
  1618. InternalError(2012110807);
  1619. grp.members[j]:=objsec;
  1620. objsec.Group:=grp;
  1621. end;
  1622. end;
  1623. end;
  1624. result:=True;
  1625. end;
  1626. class function TElfObjInput.CanReadObjData(AReader:TObjectreader):boolean;
  1627. var
  1628. header: TElfHeader;
  1629. begin
  1630. result:=false;
  1631. if AReader.Read(header,sizeof(header)) then
  1632. begin;
  1633. if (header.e_ident[EI_MAG0]=ELFMAG0) and (header.e_ident[EI_MAG1]=ELFMAG1) and
  1634. (header.e_ident[EI_MAG2]=ELFMAG2) and (header.e_ident[EI_MAG3]=ELFMAG3) then
  1635. { TODO: check additional fields }
  1636. result:=true;
  1637. end;
  1638. AReader.Seek(0);
  1639. end;
  1640. {*****************************************************************************
  1641. TElfExeOutput
  1642. *****************************************************************************}
  1643. constructor TElfExeOutput.Create;
  1644. begin
  1645. inherited Create;
  1646. CObjData:=TElfObjData;
  1647. CExeSection:=TElfExeSection;
  1648. {$ifdef cpu64}
  1649. MaxMemPos:=Qword($FFFFFFFFFFFFFFFF);
  1650. //MaxMemPos:=$7EFFFFFF; { As specified by SysV AMD64 ABI for small memory model }
  1651. {$else cpu64}
  1652. MaxMemPos:=$7FFFFFFF;
  1653. {$endif cpu64}
  1654. SectionMemAlign:=$20;
  1655. SectionDataAlign:=$20;
  1656. segmentlist:=TFPObjectList.Create(True);
  1657. neededlist:=TFPHashList.Create;
  1658. end;
  1659. destructor TElfExeOutput.Destroy;
  1660. begin
  1661. neededlist.Free;
  1662. segmentlist.Free;
  1663. dynsymlist.Free;
  1664. dynreloclist.Free;
  1665. if assigned(dynsymnames) then
  1666. FreeMem(dynsymnames);
  1667. stringdispose(FInterpreter);
  1668. inherited Destroy;
  1669. end;
  1670. function TElfExeOutput.AttachSection(objsec:TObjSection):TElfExeSection;
  1671. begin
  1672. objsec.SecOptions:=[oso_keep];
  1673. result:=TElfExeSection(FindExeSection(objsec.name));
  1674. if result=nil then
  1675. result:=TElfExeSection.Create(ExeSectionList,objsec.name);
  1676. result.AddObjSection(objsec);
  1677. end;
  1678. function TElfExeOutput.CreateSegment(atype,aflags,aalign:longword):TElfSegment;
  1679. begin
  1680. result:=TElfSegment.Create(atype,aflags,aalign);
  1681. segmentlist.add(result);
  1682. end;
  1683. procedure TElfExeOutput.WriteHeader;
  1684. var
  1685. header: TElfHeader;
  1686. begin
  1687. FillChar(header,sizeof(header),0);
  1688. header.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  1689. header.e_ident[EI_MAG1]:=ELFMAG1;
  1690. header.e_ident[EI_MAG2]:=ELFMAG2;
  1691. header.e_ident[EI_MAG3]:=ELFMAG3;
  1692. header.e_ident[EI_CLASS]:=ELFCLASS;
  1693. if target_info.endian=endian_big then
  1694. header.e_ident[EI_DATA]:=ELFDATA2MSB
  1695. else
  1696. header.e_ident[EI_DATA]:=ELFDATA2LSB;
  1697. header.e_ident[EI_VERSION]:=1;
  1698. if IsSharedLibrary then
  1699. header.e_type:=ET_DYN
  1700. else
  1701. header.e_type:=ET_EXEC;
  1702. header.e_machine:=ElfTarget.machine_code;
  1703. header.e_version:=1;
  1704. header.e_phoff:=sizeof(TElfHeader);
  1705. header.e_shoff:=shoffset;
  1706. header.e_shstrndx:=ExeSectionList.IndexOf(shstrtabsect.ExeSection)+1;
  1707. header.e_shnum:=ExeSectionList.Count+1;
  1708. header.e_phnum:=segmentlist.count;
  1709. header.e_ehsize:=sizeof(telfheader);
  1710. if assigned(EntrySym) then
  1711. header.e_entry:=EntrySym.Address;
  1712. header.e_shentsize:=sizeof(telfsechdr);
  1713. header.e_phentsize:=sizeof(telfproghdr);
  1714. MaybeSwapHeader(header);
  1715. FWriter.Write(header,sizeof(header));
  1716. end;
  1717. procedure TElfExeOutput.exesection_write_header(p:TObject;arg:Pointer);
  1718. var
  1719. shdr: TElfsechdr;
  1720. exesec: TElfExeSection absolute p;
  1721. begin
  1722. FillChar(shdr,sizeof(shdr),0);
  1723. shdr.sh_name:=exesec.shstridx;
  1724. if (ExeWriteMode=ewm_dbgonly) and
  1725. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[]) then
  1726. shdr.sh_type:=SHT_NOBITS
  1727. else
  1728. shdr.sh_type:=exesec.shtype;
  1729. shdr.sh_flags:=exesec.shflags;
  1730. if (oso_load in exesec.SecOptions) then
  1731. shdr.sh_addr:=exesec.MemPos;
  1732. shdr.sh_offset:=exesec.DataPos;
  1733. shdr.sh_size:=exesec.Size;
  1734. shdr.sh_link:=exesec.shlink;
  1735. shdr.sh_info:=exesec.shinfo;
  1736. shdr.sh_addralign:=exesec.SecAlign;
  1737. shdr.sh_entsize:=exesec.shentsize;
  1738. MaybeSwapSecHeader(shdr);
  1739. FWriter.Write(shdr,sizeof(shdr));
  1740. end;
  1741. procedure TElfExeOutput.segment_write_header(p:TObject;arg:Pointer);
  1742. var
  1743. phdr: TElfproghdr;
  1744. seg: TElfSegment absolute p;
  1745. begin
  1746. FillChar(phdr,sizeof(phdr),0);
  1747. phdr.p_type:=seg.ptype;
  1748. phdr.p_flags:=seg.pflags;
  1749. phdr.p_align:=seg.align;
  1750. phdr.p_offset:=seg.DataPos;
  1751. phdr.p_filesz:=seg.DataSize;
  1752. phdr.p_memsz:=seg.MemSize;
  1753. phdr.p_vaddr:=seg.MemPos;
  1754. phdr.p_paddr:=seg.MemPos;
  1755. MaybeSwapHeader(phdr);
  1756. FWriter.Write(phdr,sizeof(phdr));
  1757. end;
  1758. procedure TElfExeOutput.WriteStaticSymtable;
  1759. var
  1760. i: longint;
  1761. sec: TElfExeSection;
  1762. exesym: TExeSymbol;
  1763. begin
  1764. if assigned(tlsseg) then
  1765. symtab.tlsbase:=tlsseg.MemPos;
  1766. for i:=0 to ExeSectionList.Count-1 do
  1767. begin
  1768. sec:=TElfExeSection(ExeSectionList[i]);
  1769. { Must not write symbols for internal sections like .symtab }
  1770. if (sec.shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  1771. continue;
  1772. sec.secsymidx:=symtab.symidx;
  1773. symtab.writeInternalSymbol(sec.mempos,0,STT_SECTION,sec.secshidx);
  1774. end;
  1775. { local symbols first }
  1776. for i:=0 to ExeSymbolList.Count-1 do
  1777. begin
  1778. exesym:=TExeSymbol(ExeSymbolList[i]);
  1779. if (exesym.objsymbol.bind=AB_LOCAL) and (exesym.objsymbol.typ<>AT_LABEL) then
  1780. symtab.WriteSymbol(exesym.objsymbol);
  1781. end;
  1782. { Global Symbols }
  1783. for i:=0 to ExeSymbolList.Count-1 do
  1784. begin
  1785. exesym:=TExeSymbol(ExeSymbolList[i]);
  1786. if (exesym.objsymbol.bind<>AB_LOCAL) then
  1787. symtab.WriteSymbol(exesym.objsymbol);
  1788. end;
  1789. { update exe section properties }
  1790. symtab.ExeSection.size:=symtab.size;
  1791. TElfExeSection(symtab.ExeSection).shinfo:=symtab.shinfo;
  1792. TElfExeSection(symtab.ExeSection).shlink:=ExeSectionList.IndexOf(symtab.fstrsec.ExeSection)+1;
  1793. symtab.fstrsec.ExeSection.Size:=symtab.fstrsec.size;
  1794. end;
  1795. procedure TElfExeOutput.MapSectionsToSegments;
  1796. var
  1797. seg: TElfSegment;
  1798. exesec: TExeSection;
  1799. i: longint;
  1800. begin
  1801. if (not IsSharedLibrary) and assigned(interpobjsec) then
  1802. begin
  1803. phdrseg:=CreateSegment(PT_PHDR,PF_R or PF_X,sizeof(pint));
  1804. seg:=CreateSegment(PT_INTERP,PF_R,1);
  1805. seg.Add(interpobjsec.ExeSection);
  1806. end;
  1807. textseg:=CreateSegment(PT_LOAD,PF_X or PF_R,ElfTarget.max_page_size);
  1808. dataseg:=CreateSegment(PT_LOAD,PF_R or PF_W,ElfTarget.max_page_size);
  1809. for i:=0 to ExeSectionList.Count-1 do
  1810. begin
  1811. exesec:=TExeSection(ExeSectionList[i]);
  1812. if (oso_load in exesec.SecOptions) then
  1813. begin
  1814. if (TElfExeSection(exesec).shflags and SHF_TLS)<>0 then
  1815. begin
  1816. if tlsseg=nil then
  1817. tlsseg:=CreateSegment(PT_TLS,PF_R,sizeof(pint));
  1818. tlsseg.add(exesec);
  1819. end;
  1820. { TODO: at least on Linux, ld seems to drop .note.ABI-tag for static executables.
  1821. (Logic is as follows: there is no .note.ABI-tag section in ld script, so it
  1822. is processed as orphan section. As such, it is placed after .interp.
  1823. For static executables .interp is dropped, and it looks like there's nowhere to
  1824. place .note.ABI-tag in this case)
  1825. Always including it doesn't harm though (except increasing file size). }
  1826. if TElfExeSection(exesec).shtype=SHT_NOTE then
  1827. begin
  1828. if noteseg=nil then
  1829. noteseg:=CreateSegment(PT_NOTE,PF_R,4);
  1830. noteseg.Add(exesec);
  1831. Include(exesec.SecOptions,oso_debug_copy);
  1832. end;
  1833. if (oso_executable in exesec.SecOptions) or
  1834. not (oso_write in exesec.SecOptions) then
  1835. textseg.add(exesec)
  1836. else
  1837. dataseg.add(exesec);
  1838. end;
  1839. end;
  1840. if dynamiclink then
  1841. begin
  1842. seg:=CreateSegment(PT_DYNAMIC,PF_R or PF_W,sizeof(pint));
  1843. seg.add(dynamicsec.ExeSection);
  1844. end;
  1845. { stack flags }
  1846. CreateSegment(PT_GNU_STACK,PF_R or PF_W or (PF_X*ord(ExecStack)),sizeof(pint));
  1847. end;
  1848. function TElfExeOutput.AllocGOTSlot(objsym:TObjSymbol):boolean;
  1849. var
  1850. exesym: TExeSymbol;
  1851. begin
  1852. result:=false;
  1853. exesym:=objsym.exesymbol;
  1854. { Although local symbols should not be accessed through GOT,
  1855. this isn't strictly forbidden. In this case we need to fake up
  1856. the exesym to store the GOT offset in it.
  1857. TODO: name collision; maybe use a different symbol list object? }
  1858. if exesym=nil then
  1859. begin
  1860. exesym:=TExeSymbol.Create(ExeSymbolList,objsym.name+'*local*');
  1861. exesym.objsymbol:=objsym;
  1862. objsym.exesymbol:=exesym;
  1863. end;
  1864. if exesym.GotOffset>0 then
  1865. exit;
  1866. gotobjsec.alloc(sizeof(pint));
  1867. exesym.GotOffset:=gotobjsec.size;
  1868. { In shared library, every GOT entry needs a RELATIVE dynamic reloc,
  1869. imported/exported symbols need GLOB_DAT instead. For executables,
  1870. only the latter applies. }
  1871. if IsSharedLibrary or (exesym.dynindex>0) then
  1872. dynrelocsec.alloc(dynrelocsec.shentsize);
  1873. result:=true;
  1874. end;
  1875. procedure TElfExeOutput.PrepareGOT;
  1876. var
  1877. i,j,k: longint;
  1878. objsec: TElfObjSection;
  1879. exesec: TExeSection;
  1880. begin
  1881. for i:=0 to ExeSectionList.Count-1 do
  1882. begin
  1883. exesec:=TExeSection(ExeSectionList[i]);
  1884. for j:=0 to exesec.ObjSectionlist.count-1 do
  1885. begin
  1886. objsec:=TElfObjSection(exesec.ObjSectionlist[j]);
  1887. { ignore linker-generated and debug sections }
  1888. if (objsec.objdata=internalobjdata) or (oso_debug in objsec.SecOptions) then
  1889. continue;
  1890. if not objsec.Used then
  1891. internalerror(2012060901);
  1892. k:=0;
  1893. while k<objsec.ObjRelocations.Count do
  1894. begin
  1895. GOTRelocPass1(objsec,k);
  1896. inc(k);
  1897. end;
  1898. end;
  1899. end;
  1900. { remember sizes for sanity checking }
  1901. gotsize:=gotobjsec.size;
  1902. if assigned(dynrelocsec) then
  1903. dynrelsize:=dynrelocsec.size
  1904. else
  1905. dynrelsize:=0;
  1906. end;
  1907. procedure TElfExeOutput.Load_Start;
  1908. begin
  1909. inherited Load_Start;
  1910. dynsymlist:=TFPObjectList.Create(False);
  1911. gotpltobjsec:=TElfObjSection.create_ext(internalObjData,'.got.plt',
  1912. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  1913. gotobjsec:=TElfObjSection.create_ext(internalObjData,'.got',
  1914. SHT_PROGBITS,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(pint));
  1915. gotobjsec.SecOptions:=[oso_keep];
  1916. { GOT symbol and reserved .got.plt entries }
  1917. internalObjData.SetSection(gotpltobjsec);
  1918. gotsymbol:=internalObjData.SymbolDefine('_GLOBAL_OFFSET_TABLE_',AB_GLOBAL,AT_DATA);
  1919. gotpltobjsec.writeZeros(3*sizeof(pint));
  1920. end;
  1921. procedure TElfExeOutput.Load_DynamicObject(objdata:TObjData;asneeded:boolean);
  1922. var
  1923. i: longint;
  1924. exesym: TExeSymbol;
  1925. objsym: TObjSymbol;
  1926. needed: boolean;
  1927. begin
  1928. Comment(v_debug,'Dynamic object: '+objdata.name);
  1929. needed:=false;
  1930. for i:=0 to UnresolvedExeSymbols.Count-1 do
  1931. begin
  1932. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  1933. if not (exesym.State in [symstate_undefined,symstate_undefweak]) then
  1934. continue;
  1935. objsym:=TObjSymbol(objdata.ObjSymbolList.Find(exesym.name));
  1936. if assigned(objsym) then
  1937. begin
  1938. exesym.State:=symstate_defined;
  1939. exesym.dynindex:=dynsymlist.Add(exesym)+1;
  1940. needed:=true;
  1941. end;
  1942. end;
  1943. if (needed or (not asneeded)) and
  1944. (neededlist.Find(objdata.name)=nil) then
  1945. neededlist.Add(objdata.name,objdata);
  1946. end;
  1947. procedure TElfExeOutput.Order_Start;
  1948. begin
  1949. inherited Order_Start;
  1950. dynamiclink:=IsSharedLibrary or (dynsymlist.count>0) or
  1951. (
  1952. (UnresolvedExeSymbols.Count>0) and
  1953. not (cs_link_staticflag in current_settings.globalswitches)
  1954. );
  1955. if dynamiclink then
  1956. InitDynlink;
  1957. if dynamiclink or (IndirectObjSymbols.Count>0) then
  1958. CreatePLT;
  1959. end;
  1960. procedure TElfExeOutput.Order_end;
  1961. procedure set_oso_keep(const s:string);
  1962. var
  1963. exesec:TExeSection;
  1964. objsec:TObjSection;
  1965. i:longint;
  1966. begin
  1967. exesec:=TExeSection(ExeSectionList.Find(s));
  1968. if assigned(exesec) then
  1969. begin
  1970. for i:=0 to exesec.ObjSectionList.Count-1 do
  1971. begin
  1972. objsec:=TObjSection(exesec.ObjSectionList[i]);
  1973. { ignore sections used for symbol definition }
  1974. if oso_data in objsec.SecOptions then
  1975. objsec.SecOptions:=[oso_keep];
  1976. end;
  1977. end;
  1978. end;
  1979. begin
  1980. inherited Order_end;
  1981. set_oso_keep('.init');
  1982. set_oso_keep('.fini');
  1983. set_oso_keep('.jcr');
  1984. set_oso_keep('.ctors');
  1985. set_oso_keep('.dtors');
  1986. set_oso_keep('.preinit_array');
  1987. set_oso_keep('.init_array');
  1988. set_oso_keep('.fini_array');
  1989. set_oso_keep('.eh_frame');
  1990. { let .dynamic reference other dynamic sections so they aren't marked
  1991. for removal as unused }
  1992. if dynamiclink then
  1993. WriteDynamicTags;
  1994. end;
  1995. procedure TElfExeOutput.AfterUnusedSectionRemoval;
  1996. var
  1997. i:longint;
  1998. exesym:TExeSymbol;
  1999. objsym:TObjSymbol;
  2000. objsec: TObjSection;
  2001. begin
  2002. { Unused section removal sets Used property of referenced exesymbols.
  2003. Remaining ones can be removed. }
  2004. for i:=0 to dynsymlist.count-1 do
  2005. begin
  2006. exesym:=TExeSymbol(dynsymlist[i]);
  2007. if assigned(exesym.ObjSymbol.ObjSection) then // an exported symbol
  2008. continue;
  2009. if not exesym.used then
  2010. begin
  2011. dynsymlist[i]:=nil;
  2012. exesym.dynindex:=0;
  2013. end;
  2014. end;
  2015. dynsymlist.Pack;
  2016. { reindex }
  2017. for i:=0 to dynsymlist.count-1 do
  2018. TExeSymbol(dynsymlist[i]).dynindex:=i+1;
  2019. { Drop unresolved symbols that aren't referenced, assign dynamic
  2020. indices to remaining ones, but not if linking with -Xt.
  2021. TODO: behavior of .so with -Xt ? }
  2022. if (cs_link_staticflag in current_settings.globalswitches) then
  2023. UnresolvedExeSymbols.Clear
  2024. else
  2025. for i:=0 to UnresolvedExeSymbols.Count-1 do
  2026. begin
  2027. exesym:=TExeSymbol(UnresolvedExeSymbols[i]);
  2028. if exesym.used then
  2029. begin
  2030. if exesym.dynindex<>0 then
  2031. InternalError(2012062301);
  2032. exesym.dynindex:=dynsymlist.add(exesym)+1;
  2033. exesym.state:=symstate_defined;
  2034. end
  2035. else
  2036. UnresolvedExeSymbols[i]:=nil;
  2037. end;
  2038. UnresolvedExeSymbols.Pack;
  2039. { Scan relocations to determine size of GOT, dynamic reloc section, etc. }
  2040. PrepareGOT;
  2041. { Write required PLT entries }
  2042. for i:=0 to dynsymlist.Count-1 do
  2043. begin
  2044. exesym:=TExeSymbol(dynsymlist[i]);
  2045. if assigned(exesym.ObjSymbol.objsection) then // an exported symbol
  2046. continue;
  2047. { if there's no PLT references to symbol, then PLT entry isn't needed }
  2048. { !! Does not work correctly yet !! }
  2049. // if (exesym.objsymbol.refs and symref_plt)=0 then
  2050. // continue;
  2051. { This symbol has a valid address to which relocations are resolved,
  2052. but it remains (weak)external when written to dynamic symtable. }
  2053. objsym:=internalobjdata.CreateSymbol(exesym.name);
  2054. objsym.typ:=AT_FUNCTION;
  2055. objsym.bind:=exesym.ObjSymbol.bind; { AB_EXTERNAL or AB_WEAK_EXTERNAL }
  2056. objsym.offset:=pltobjsec.size;
  2057. objsym.objsection:=pltobjsec;
  2058. exesym.ObjSymbol:=objsym;
  2059. WritePLTEntry(exesym);
  2060. end;
  2061. { Handle indirect symbols }
  2062. for i:=0 to IndirectObjSymbols.Count-1 do
  2063. begin
  2064. objsym:=TObjSymbol(IndirectObjSymbols[i]);
  2065. objsec:=objsym.ExeSymbol.ObjSymbol.objsection;
  2066. objsym.bind:=AB_EXTERNAL; { cheat FixupSymbols }
  2067. if (oso_plt in objsec.SecOptions) then
  2068. continue;
  2069. WriteIndirectPLTEntry(objsym.ExeSymbol);
  2070. end;
  2071. FixupSymbols;
  2072. if dynamiclink then
  2073. WriteDynamicSymbolsHash;
  2074. { Create .shstrtab section, which is needed in both exe and .dbg files }
  2075. shstrtabsect:=TElfObjSection.Create_ext(internalObjData,'.shstrtab',SHT_STRTAB,0,1,0);
  2076. shstrtabsect.SecOptions:=[oso_debug_copy];
  2077. AttachSection(shstrtabsect);
  2078. { Create the static symtable (.symtab and .strtab) }
  2079. if (cs_link_separate_dbg_file in current_settings.globalswitches) or
  2080. not(cs_link_strip in current_settings.globalswitches) then
  2081. begin
  2082. symtab:=TElfSymtab.Create(internalObjData,esk_exe);
  2083. symtab.SecOptions:=[oso_debug];
  2084. symtab.fstrsec.SecOptions:=[oso_debug];
  2085. AttachSection(symtab);
  2086. AttachSection(symtab.fstrsec);
  2087. end;
  2088. { Re-enable sections which end up to contain some data
  2089. (.got, .rel[a].dyn, .rel[a].plt (includes .rel[a].iplt) and .hash }
  2090. if gotobjsec.size<>0 then
  2091. Exclude(gotobjsec.ExeSection.SecOptions,oso_disabled);
  2092. if assigned(dynrelocsec) and (dynrelocsec.size<>0) then
  2093. Exclude(dynrelocsec.ExeSection.SecOptions,oso_disabled);
  2094. if assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2095. Exclude(pltrelocsec.ExeSection.SecOptions,oso_disabled);
  2096. if assigned(ipltrelocsec) and (ipltrelocsec.size>0) then
  2097. Exclude(ipltrelocsec.ExeSection.SecOptions,oso_disabled);
  2098. if assigned(hashobjsec) then
  2099. Exclude(hashobjsec.ExeSection.SecOptions,oso_disabled);
  2100. if assigned(symversec) and (symversec.size<>0) then
  2101. Exclude(symversec.ExeSection.SecOptions,oso_disabled);
  2102. if assigned(verneedsec) and (verneedsec.size<>0) then
  2103. Exclude(verneedsec.ExeSection.SecOptions,oso_disabled);
  2104. if assigned(verdefsec) and (verdefsec.size<>0) then
  2105. Exclude(verneedsec.ExeSection.SecOptions,oso_disabled);
  2106. RemoveDisabledSections;
  2107. MapSectionsToSegments;
  2108. if dynamiclink then
  2109. FinishDynamicTags;
  2110. end;
  2111. procedure TElfExeOutput.MemPos_Start;
  2112. var
  2113. i,j: longint;
  2114. dynstrndx,dynsymndx: longword;
  2115. seg: TElfSegment;
  2116. exesec: TElfExeSection;
  2117. objsec: TObjSection;
  2118. tempmempos: qword;
  2119. begin
  2120. { Remove any existing .shstrtab contents }
  2121. if (shstrtabsect.size>0) then
  2122. begin
  2123. shstrtabsect.ReleaseData;
  2124. shstrtabsect.Size:=0;
  2125. shstrtabsect.SecOptions:=[oso_data];
  2126. end;
  2127. { Assign section indices and fill .shstrtab
  2128. List of sections cannot be modified after this point. }
  2129. shstrtabsect.writezeros(1);
  2130. for i:=0 to ExeSectionList.Count-1 do
  2131. begin
  2132. exesec:=TElfExeSection(ExeSectionList[i]);
  2133. exesec.shstridx:=shstrtabsect.writestr(exesec.Name);
  2134. exesec.secshidx:=i+1;
  2135. end;
  2136. if dynamiclink then
  2137. begin
  2138. { fixup sh_link/sh_info members of various dynamic sections }
  2139. dynstrndx:=TElfExeSection(dynsymtable.fstrsec.ExeSection).secshidx;
  2140. dynsymndx:=TElfExeSection(dynsymtable.ExeSection).secshidx;
  2141. TElfExeSection(hashobjsec.ExeSection).shlink:=dynsymndx;
  2142. TElfExeSection(dynamicsec.ExeSection).shlink:=dynstrndx;
  2143. TElfExeSection(dynsymtable.ExeSection).shlink:=dynstrndx;
  2144. if assigned(pltrelocsec) then
  2145. begin
  2146. TElfExeSection(pltrelocsec.ExeSection).shlink:=dynsymndx;
  2147. TElfExeSection(pltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2148. end;
  2149. if assigned(dynrelocsec) and assigned(dynrelocsec.ExeSection) then
  2150. TElfExeSection(dynrelocsec.ExeSection).shlink:=dynsymndx;
  2151. if symversec.size>0 then
  2152. TElfExeSection(symversec.ExeSection).shlink:=dynsymndx;
  2153. if verdefsec.size>0 then
  2154. TElfExeSection(verdefsec.ExeSection).shlink:=dynstrndx;
  2155. if verneedsec.size>0 then
  2156. TElfExeSection(verneedsec.ExeSection).shlink:=dynstrndx;
  2157. end
  2158. else if assigned(ipltrelocsec) then
  2159. TElfExeSection(ipltrelocsec.ExeSection).shinfo:=TElfExeSection(pltobjsec.ExeSection).secshidx;
  2160. { The actual layout }
  2161. if IsSharedLibrary then
  2162. CurrMemPos:=0
  2163. else
  2164. CurrMemPos:=ElfTarget.exe_image_base;
  2165. textseg.MemPos:=CurrMemPos;
  2166. if assigned(phdrseg) then
  2167. begin
  2168. phdrseg.Mempos:=CurrMemPos+sizeof(TElfHeader);
  2169. phdrseg.Memsize:=sizeof(TElfproghdr)*segmentlist.count;
  2170. end;
  2171. CurrMemPos:=CurrMemPos+sizeof(TElfHeader)+segmentlist.count*sizeof(TElfproghdr);
  2172. MemPos_Segment(textseg);
  2173. CurrMemPos:=Align(CurrMemPos,SectionDataAlign); {! Data,not MemAlign}
  2174. CurrMemPos:=CurrMemPos+ElfTarget.max_page_size;
  2175. dataseg.MemPos:=CurrMemPos;
  2176. MemPos_Segment(dataseg);
  2177. { Mempos of unmapped sections is forced to zero, but we have to set positions
  2178. of its objsections and update sizes }
  2179. for i:=0 to ExeSectionList.Count-1 do
  2180. begin
  2181. exesec:=TElfExeSection(ExeSectionList[i]);
  2182. if not (oso_load in exesec.SecOptions) then
  2183. begin
  2184. tempmempos:=0;
  2185. exesec.MemPos:=tempmempos;
  2186. for j:=0 to exesec.ObjSectionList.Count-1 do
  2187. begin
  2188. objsec:=TObjSection(exesec.ObjSectionList[j]);
  2189. tempmempos:=objsec.setmempos(tempmempos);
  2190. end;
  2191. exesec.Size:=tempmempos;
  2192. end;
  2193. end;
  2194. { Update MemPos and MemSize of non-load segments,
  2195. in particular, TLS sizes are needed to resolve relocations }
  2196. for i:=0 to segmentlist.count-1 do
  2197. begin
  2198. seg:=TElfSegment(segmentlist[i]);
  2199. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2200. continue;
  2201. seg.MemPos:=TExeSection(seg.FSectionList.First).MemPos;
  2202. for j:=0 to seg.FSectionList.Count-1 do
  2203. begin
  2204. exesec:=TElfExeSection(seg.FSectionList[j]);
  2205. seg.MemSize:=exesec.MemPos+exesec.Size-seg.MemPos;
  2206. end;
  2207. end;
  2208. if (not gotwritten) then
  2209. begin
  2210. { reset size of .got and .rel[a].dyn, they will be refilled while fixing up relocations }
  2211. if assigned(gotobjsec) then
  2212. gotobjsec.size:=0;
  2213. if assigned(dynrelocsec) then
  2214. begin
  2215. dynrelocsec.size:=0;
  2216. { write actual .dynsym content (needs valid symbol addresses) }
  2217. if assigned(tlsseg) then
  2218. dynsymtable.tlsbase:=tlsseg.MemPos;
  2219. dynsymtable.size:=sizeof(TElfsymbol);
  2220. for i:=0 to dynsymlist.count-1 do
  2221. dynsymtable.writeSymbol(TExeSymbol(dynsymlist[i]).objsymbol,dynsymnames[i]);
  2222. end;
  2223. end;
  2224. end;
  2225. procedure TElfExeOutput.MemPos_Segment(seg:TElfSegment);
  2226. var
  2227. i: longint;
  2228. exesec: TElfExeSection;
  2229. begin
  2230. for i:=0 to seg.FSectionList.Count-1 do
  2231. begin
  2232. exesec:=TElfExeSection(seg.FSectionList[i]);
  2233. inherited MemPos_ExeSection(exesec);
  2234. { .tbss should not contribute to address space }
  2235. if (exesec.shtype=SHT_NOBITS) and ((exesec.shflags and SHF_TLS)<>0) then
  2236. CurrMemPos:=exesec.MemPos;
  2237. end;
  2238. { calculate size of the segment }
  2239. seg.MemSize:=CurrMemPos-seg.MemPos;
  2240. end;
  2241. procedure TElfExeOutput.MemPos_ExeSection(const aname:string);
  2242. begin
  2243. // Ignore. All layout is done in mempos_start
  2244. end;
  2245. procedure TElfExeOutput.DataPos_Start;
  2246. var
  2247. i,j: longint;
  2248. exesec: TExeSection;
  2249. seg: TElfSegment;
  2250. objreloc: TObjRelocation;
  2251. begin
  2252. gotwritten:=true;
  2253. { If target does not support sorted relocations, it is expected to write the
  2254. entire .rel[a].dyn section during FixupRelocations, and leave dynreloclist empty.
  2255. Otherwise, only RELATIVE ones should be written, space for non-relative relocations
  2256. should remain. }
  2257. if assigned(dynrelocsec) then
  2258. begin
  2259. if (dynrelocsec.size+(dynreloclist.count*dynrelocsec.shentsize)<>dynrelsize) then
  2260. InternalError(2012110601);
  2261. { Write out non-RELATIVE dynamic relocations
  2262. TODO: additional sorting? }
  2263. for i:=0 to dynreloclist.count-1 do
  2264. begin
  2265. objreloc:=TObjRelocation(dynreloclist[i]);
  2266. WriteDynRelocEntry(objreloc.dataoffset,objreloc.ftype,objreloc.symbol.exesymbol.dynindex,0);
  2267. end;
  2268. end;
  2269. { sanity checks }
  2270. if assigned(gotobjsec) and (gotsize<>gotobjsec.size) then
  2271. InternalError(2012092501);
  2272. if assigned(dynrelocsec) and (dynrelsize<>dynrelocsec.size) then
  2273. InternalError(2012092502);
  2274. if (ExeWriteMode=ewm_dbgonly) or
  2275. (
  2276. (ExeWriteMode=ewm_exefull) and
  2277. not(cs_link_strip in current_settings.globalswitches)
  2278. ) then
  2279. WriteStaticSymtable;
  2280. { first handle primary segments }
  2281. textseg.DataPos:=0;
  2282. CurrDataPos:=sizeof(TElfHeader)+sizeof(TElfproghdr)*segmentlist.count;
  2283. if assigned(phdrseg) then
  2284. begin
  2285. phdrseg.DataPos:=sizeof(TElfHeader);
  2286. phdrseg.DataSize:=sizeof(TElfproghdr)*segmentlist.count;
  2287. end;
  2288. DataPos_Segment(textseg);
  2289. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2290. dataseg.DataPos:=CurrDataPos;
  2291. DataPos_Segment(dataseg);
  2292. { then unmapped sections }
  2293. for i:=0 to ExeSectionList.Count-1 do
  2294. begin
  2295. exesec:=TExeSection(ExeSectionList[i]);
  2296. if not (oso_load in exesec.SecOptions) then
  2297. inherited DataPos_ExeSection(exesec);
  2298. end;
  2299. { finally, update size/position of non-load segments }
  2300. for i:=0 to segmentlist.count-1 do
  2301. begin
  2302. seg:=TElfSegment(segmentlist[i]);
  2303. if (seg.ptype=PT_LOAD) or (seg.FSectionList.Count=0) then
  2304. continue;
  2305. seg.DataPos:=TExeSection(seg.FSectionList.First).DataPos;
  2306. for j:=0 to seg.FSectionList.Count-1 do
  2307. begin
  2308. exesec:=TExeSection(seg.FSectionList[j]);
  2309. if oso_data in exesec.SecOptions then
  2310. seg.DataSize:=exesec.DataPos+exesec.Size-seg.DataPos;
  2311. end;
  2312. end;
  2313. { place section headers after the data }
  2314. shoffset:=CurrDataPos;
  2315. CurrDataPos:=CurrDataPos+ExeSectionList.Count*sizeof(TElfsechdr);
  2316. end;
  2317. procedure TElfExeOutput.DataPos_Segment(seg:TElfSegment);
  2318. var
  2319. i: longint;
  2320. exesec: TElfExeSection;
  2321. begin
  2322. for i:=0 to seg.FSectionList.Count-1 do
  2323. begin
  2324. exesec:=TElfExeSection(seg.FSectionList[i]);
  2325. { ELF needs DataPos set to 'would-be' value for sections that
  2326. don't have data, and for non-debug sections in .dbg file, too.
  2327. This slightly differs from generic approach. }
  2328. if not (oso_data in exesec.SecOptions) or
  2329. (
  2330. (ExeWriteMode=ewm_dbgonly) and
  2331. (exesec.SecOptions*[oso_debug,oso_debug_copy]=[])
  2332. ) then
  2333. begin
  2334. CurrDataPos:=align(CurrDataPos,SectionDataAlign);
  2335. exesec.DataPos:=CurrDataPos;
  2336. end
  2337. else
  2338. inherited DataPos_ExeSection(exesec);
  2339. end;
  2340. { calculate size of the segment }
  2341. seg.DataSize:=CurrDataPos-seg.DataPos;
  2342. end;
  2343. procedure TElfExeOutput.DataPos_ExeSection(const aname:string);
  2344. begin
  2345. // Ignore. Work is done entirely in datapos_start.
  2346. end;
  2347. procedure TElfExeOutput.InitDynlink;
  2348. begin
  2349. if not IsSharedLibrary then
  2350. begin
  2351. interpobjsec:=internalObjData.createsection('.interp',1,[oso_data,oso_load,oso_keep]);
  2352. interpobjsec.writestr(interpreter^);
  2353. end;
  2354. hashobjsec:=TElfObjSection.create_ext(internalObjData,'.hash',
  2355. SHT_HASH,SHF_ALLOC,sizeof(pint),4);
  2356. hashobjsec.secoptions:=[oso_keep];
  2357. dynsymtable:=TElfSymtab.create(internalObjData,esk_dyn);
  2358. dynamicsec:=TElfObjSection.create_ext(internalObjData,'.dynamic',
  2359. SHT_DYNAMIC,SHF_ALLOC or SHF_WRITE,sizeof(pint),sizeof(TElfDyn));
  2360. dynamicsec.SecOptions:=[oso_keep];
  2361. dynrelocsec:=TElfObjSection.create_reloc(internalObjData,'.dyn',true);
  2362. dynrelocsec.SecOptions:=[oso_keep];
  2363. dynreloclist:=TFPObjectList.Create(true);
  2364. symversec:=TElfObjSection.create_ext(internalObjData,'.gnu.version',
  2365. SHT_GNU_VERSYM,SHF_ALLOC,sizeof(word),sizeof(word));
  2366. symversec.SecOptions:=[oso_keep];
  2367. verdefsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_d',
  2368. SHT_GNU_VERDEF,SHF_ALLOC,sizeof(pint),0);
  2369. verdefsec.SecOptions:=[oso_keep];
  2370. verneedsec:=TElfObjSection.create_ext(internalObjData,'.gnu.version_r',
  2371. SHT_GNU_VERNEED,SHF_ALLOC,sizeof(pint),0);
  2372. verneedsec.SecOptions:=[oso_keep];
  2373. end;
  2374. const
  2375. hashbuckets: array[0..15] of longint=(
  2376. 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
  2377. 16411, 32771);
  2378. {$push}{$r-,q-}
  2379. function elfhash(const name:string):longword;
  2380. var
  2381. g: longword;
  2382. i: longint;
  2383. begin
  2384. result:=0;
  2385. for i:=1 to length(name) do
  2386. begin
  2387. result:=(result shl 4)+ord(name[i]);
  2388. g:=result and $F0000000;
  2389. if g>0 then
  2390. result:=result xor (g shr 24);
  2391. result:=result and (not g);
  2392. end;
  2393. end;
  2394. {$pop}
  2395. procedure TElfExeOutput.WriteDynamicSymbolsHash;
  2396. var
  2397. nchains,nbuckets: longint;
  2398. i,j: longint;
  2399. hashdata: plongint;
  2400. sym: TExeSymbol;
  2401. begin
  2402. dynsymnames:=AllocMem(dynsymlist.count*sizeof(longword));
  2403. nchains:=dynsymlist.Count+1;
  2404. { determine suitable bucket count }
  2405. i:=high(hashbuckets);
  2406. while (i>=0) and (nchains<hashbuckets[i]) do
  2407. dec(i);
  2408. nbuckets:=hashbuckets[i];
  2409. hashdata:=AllocMem((2+nchains+nbuckets)*sizeof(longint));
  2410. hashdata[0]:=nbuckets;
  2411. hashdata[1]:=nchains;
  2412. { The contents of .dynsym can be written only after mempos pass
  2413. because it needs valid symbol virtual addresses and section indices.
  2414. Here we preset .dynsym size and write names, in order to get
  2415. correct size of .dynstr section. }
  2416. dynsymtable.size:=(dynsymlist.count+1)*sizeof(TElfsymbol);
  2417. for i:=0 to dynsymlist.Count-1 do
  2418. begin
  2419. sym:=TExeSymbol(dynsymlist[i]);
  2420. dynsymnames[i]:=dynsymtable.fstrsec.writestr(sym.objsymbol.name);
  2421. j:=(elfhash(sym.objsymbol.name) mod nbuckets)+2;
  2422. while hashdata[j]<>0 do
  2423. j:=2+nbuckets+hashdata[j];
  2424. hashdata[j]:=i+1;
  2425. end;
  2426. if source_info.endian<>target_info.endian then
  2427. for i:=0 to nchains+nbuckets+1 do
  2428. hashdata[i]:=swapendian(hashdata[i]);
  2429. hashobjsec.write(hashdata^,(2+nchains+nbuckets)*sizeof(longint));
  2430. freemem(hashdata);
  2431. end;
  2432. procedure TElfExeOutput.WriteDynRelocEntry(dataofs:aword;typ:byte;symidx:aword;addend:aword);
  2433. var
  2434. rel:telfreloc;
  2435. begin
  2436. rel.address:=dataofs;
  2437. rel.info:=ELF_R_INFO(symidx,typ);
  2438. rel.addend:=addend;
  2439. MaybeSwapElfReloc(rel);
  2440. dynrelocsec.write(rel,dynrelocsec.shentsize);
  2441. end;
  2442. procedure TElfExeOutput.WriteDynTag(aTag:longword;aValue:longword);
  2443. var
  2444. d: TElfDyn;
  2445. begin
  2446. d.d_tag:=aTag;
  2447. d.d_val:=aValue;
  2448. MaybeSwapElfDyn(d);
  2449. dynamicsec.write(d,sizeof(TElfDyn));
  2450. end;
  2451. procedure TElfExeOutput.WriteDynTag(aTag:longword;aSection:TObjSection;aOffs:aword);
  2452. var
  2453. d: TElfDyn;
  2454. begin
  2455. d.d_tag:=aTag;
  2456. if source_info.endian<>target_info.endian then
  2457. d.d_tag:=swapendian(d.d_tag);
  2458. dynamicsec.write(d.d_tag,sizeof(d.d_tag));
  2459. { TODO: ignores endianness! }
  2460. dynamicsec.writeReloc_internal(aSection,aOffs,sizeof(d.d_ptr),RELOC_ABSOLUTE);
  2461. end;
  2462. procedure TElfExeOutput.WriteDynamicTags;
  2463. var
  2464. s: aword;
  2465. i: longint;
  2466. sym: TExeSymbol;
  2467. hs:string;
  2468. begin
  2469. for i:=0 to neededlist.Count-1 do
  2470. begin
  2471. s:=dynsymtable.fstrsec.writestr(neededlist.NameOfIndex(i));
  2472. WriteDynTag(DT_NEEDED,s);
  2473. end;
  2474. if IsSharedLibrary then
  2475. begin
  2476. s:=dynsymtable.fstrsec.writestr(ExtractFileName(current_module.sharedlibfilename));
  2477. WriteDynTag(DT_SONAME,s);
  2478. { TODO: names hardcoded here }
  2479. sym:=TExeSymbol(ExeSymbolList.Find('FPC_SHARED_LIB_START'));
  2480. if assigned(sym) then
  2481. WriteDynTag(DT_INIT,sym.objsymbol.objsection,sym.objsymbol.offset);
  2482. sym:=TExeSymbol(ExeSymbolList.Find('FPC_LIB_EXIT'));
  2483. if assigned(sym) then
  2484. WriteDynTag(DT_FINI,sym.objsymbol.objsection,sym.objsymbol.offset);
  2485. end;
  2486. { TODO: we need a dedicated parameter to pass runpath, instead of this hack
  2487. (-Xr is a different thing, it passes "-rpath-link"). }
  2488. if (ParaLinkOptions<>'') then
  2489. begin
  2490. hs:=ParaLinkOptions;
  2491. while (hs<>'') do
  2492. begin
  2493. if (GetToken(hs,' ')='-rpath') then
  2494. begin
  2495. s:=dynsymtable.fstrsec.writestr(GetToken(hs,' '));
  2496. WriteDynTag(DT_RPATH,s);
  2497. end;
  2498. end;
  2499. end;
  2500. writeDynTag(DT_HASH,hashobjsec);
  2501. writeDynTag(DT_STRTAB,dynsymtable.fstrsec);
  2502. writeDynTag(DT_SYMTAB,dynsymtable);
  2503. writeDynTag(DT_SYMENT,sizeof(TElfSymbol));
  2504. if Assigned(gotpltobjsec) then
  2505. writeDynTag(DT_PLTGOT,gotpltobjsec);
  2506. end;
  2507. const
  2508. pltreltags: array[boolean] of longword=(DT_REL,DT_RELA);
  2509. relsztags: array[boolean] of longword=(DT_RELSZ,DT_RELASZ);
  2510. relenttags: array[boolean] of longword=(DT_RELENT,DT_RELAENT);
  2511. relcnttags: array[boolean] of longword=(DT_RELCOUNT,DT_RELACOUNT);
  2512. procedure TElfExeOutput.FinishDynamicTags;
  2513. var
  2514. rela: boolean;
  2515. begin
  2516. if assigned(dynsymtable) then
  2517. writeDynTag(DT_STRSZ,dynsymtable.fstrsec.size);
  2518. if hastextrelocs then
  2519. writeDynTag(DT_TEXTREL,0);
  2520. if Assigned(pltrelocsec) and (pltrelocsec.size>0) then
  2521. begin
  2522. writeDynTag(DT_PLTRELSZ,pltrelocsec.Size);
  2523. writeDynTag(DT_PLTREL,pltreltags[pltrelocsec.shtype=SHT_RELA]);
  2524. writeDynTag(DT_JMPREL,pltrelocsec);
  2525. end;
  2526. if Assigned(dynrelocsec) and (dynrelocsec.size>0) then
  2527. begin
  2528. rela:=(dynrelocsec.shtype=SHT_RELA);
  2529. writeDynTag(pltreltags[rela],dynrelocsec);
  2530. writeDynTag(relsztags[rela],dynrelocsec.Size);
  2531. writeDynTag(relenttags[rela],dynrelocsec.shentsize);
  2532. if (relative_reloc_count>0) then
  2533. writeDynTag(relcnttags[rela],relative_reloc_count);
  2534. end;
  2535. if (verdefcount>0) or (verneedcount>0) then
  2536. begin
  2537. if (verdefcount>0) then
  2538. begin
  2539. writeDynTag(DT_VERDEF,verdefsec);
  2540. writeDynTag(DT_VERDEFNUM,verdefcount);
  2541. end;
  2542. if (verneedcount>0) then
  2543. begin
  2544. writeDynTag(DT_VERNEED,verneedsec);
  2545. writeDynTag(DT_VERNEEDNUM,verneedcount);
  2546. end;
  2547. writeDynTag(DT_VERSYM,symversec);
  2548. end;
  2549. writeDynTag(DT_NULL,0);
  2550. end;
  2551. procedure TElfExeOutput.CreatePLT;
  2552. var
  2553. reloc: TObjRelocation;
  2554. begin
  2555. pltobjsec:=TElfObjSection.create_ext(internalObjData,'.plt',
  2556. SHT_PROGBITS,SHF_ALLOC or SHF_EXECINSTR,4,16);
  2557. pltobjsec.SecOptions:=[oso_keep,oso_plt];
  2558. pltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.plt',true);
  2559. pltrelocsec.SecOptions:=[oso_keep];
  2560. ipltrelocsec:=TElfObjSection.create_reloc(internalObjData,'.iplt',true);
  2561. ipltrelocsec.SecOptions:=[oso_keep];
  2562. { reference .dynamic from .got.plt, this isn't necessary if linking statically }
  2563. { TODO: maybe move writing initial .got.plt entries here completely
  2564. (needs testing --- GOT symbol may get lost if .got.plt is empty)}
  2565. if dynamiclink then
  2566. begin
  2567. reloc:=TObjRelocation.CreateSection(0,dynamicsec,RELOC_ABSOLUTE);
  2568. reloc.size:=sizeof(pint);
  2569. gotpltobjsec.ObjRelocations.Add(reloc);
  2570. end;
  2571. { Initial PLT entry, CPU-specific }
  2572. WriteFirstPLTEntry;
  2573. end;
  2574. procedure TElfExeOutput.WritePLTEntry(exesym:TExeSymbol);
  2575. begin
  2576. // must be implemented by CPU-specific descendant
  2577. InternalError(2012092102);
  2578. end;
  2579. procedure TElfExeOutput.WriteIndirectPLTEntry(exesym:TExeSymbol);
  2580. begin
  2581. // must be implemented by CPU-specific descendant
  2582. InternalError(2012092101);
  2583. end;
  2584. function TElfExeOutput.WriteData:boolean;
  2585. begin
  2586. WriteHeader;
  2587. segmentlist.ForEachCall(@segment_write_header,nil);
  2588. WriteExeSectionContent;
  2589. FWriter.WriteZeros(sizeof(TElfsechdr));
  2590. ExeSectionList.ForEachCall(@exesection_write_header,nil);
  2591. result:=true;
  2592. end;
  2593. procedure TElfExeOutput.GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);
  2594. var
  2595. exportlist: TCmdStrList;
  2596. sym: TExeSymbol;
  2597. begin
  2598. AllowUndefinedSymbols:=IsSharedLibrary;
  2599. { add exported symbols to dynamic list }
  2600. exportlist:=texportlibunix(exportlib).exportedsymnames;
  2601. if not exportlist.empty then
  2602. repeat
  2603. sym:=TExeSymbol(ExeSymbolList.Find(exportlist.getfirst));
  2604. if assigned(sym) then
  2605. begin
  2606. if assigned(sym.objsymbol.objsection) then
  2607. sym.objsymbol.objsection.SecOptions:=[oso_keep];
  2608. sym.dynindex:=dynsymlist.add(sym)+1
  2609. end
  2610. else
  2611. InternalError(2012071801);
  2612. until exportlist.empty;
  2613. end;
  2614. procedure TElfExeOutput.ReportNonDSOReloc(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2615. begin
  2616. { TODO: include objsec properties into message }
  2617. Comment(v_error,'Relocation '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+''' cannot be used when linking a shared object; recompile with -Cg');
  2618. end;
  2619. procedure TElfExeOutput.ReportRelocOverflow(reltyp:byte;objsec:TObjSection;ObjReloc:TObjRelocation);
  2620. begin
  2621. { TODO: include objsec properties into message }
  2622. Comment(v_error,'Relocation truncated to fit: '+ElfTarget.RelocName(reltyp)+' against '''+objreloc.TargetName+'''');
  2623. end;
  2624. {****************************************************************************
  2625. TElfExeSection
  2626. ****************************************************************************}
  2627. procedure TElfExeSection.AddObjSection(objsec:TObjSection;ignoreprops:boolean);
  2628. begin
  2629. inherited AddObjSection(objsec,ignoreprops);
  2630. if ignoreprops then
  2631. exit;
  2632. if (shtype=SHT_NULL) then
  2633. begin
  2634. shtype:=TElfObjSection(objsec).shtype;
  2635. shflags:=TElfObjSection(objsec).shflags;
  2636. shentsize:=TElfObjSection(objsec).shentsize;
  2637. end;
  2638. end;
  2639. {****************************************************************************
  2640. TElfSegment
  2641. ****************************************************************************}
  2642. constructor TElfSegment.Create(atype,aflags,aalign:longword);
  2643. begin
  2644. ptype:=atype;
  2645. pflags:=aflags;
  2646. align:=aalign;
  2647. FSectionList:=TFPObjectList.Create(false);
  2648. end;
  2649. destructor TElfSegment.Destroy;
  2650. begin
  2651. FSectionList.Free;
  2652. inherited Destroy;
  2653. end;
  2654. procedure TElfSegment.Add(exesec:TExeSection);
  2655. begin
  2656. FSectionList.Add(exesec);
  2657. end;
  2658. end.