ogelf.pas 104 KB

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