tccelf.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  1. /*
  2. * ELF file handling for TCC
  3. *
  4. * Copyright (c) 2001-2004 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifdef TCC_TARGET_X86_64
  21. #define ElfW_Rel ElfW(Rela)
  22. #define SHT_RELX SHT_RELA
  23. #define REL_SECTION_FMT ".rela%s"
  24. /* x86-64 requires PLT for DLLs */
  25. #define TCC_OUTPUT_DLL_WITH_PLT
  26. #else
  27. #define ElfW_Rel ElfW(Rel)
  28. #define SHT_RELX SHT_REL
  29. #define REL_SECTION_FMT ".rel%s"
  30. #endif
  31. /* XXX: DLL with PLT would only work with x86-64 for now */
  32. //#define TCC_OUTPUT_DLL_WITH_PLT
  33. static int put_elf_str(Section *s, const char *sym)
  34. {
  35. int offset, len;
  36. char *ptr;
  37. len = strlen(sym) + 1;
  38. offset = s->data_offset;
  39. ptr = section_ptr_add(s, len);
  40. memcpy(ptr, sym, len);
  41. return offset;
  42. }
  43. /* elf symbol hashing function */
  44. static unsigned long elf_hash(const unsigned char *name)
  45. {
  46. unsigned long h = 0, g;
  47. while (*name) {
  48. h = (h << 4) + *name++;
  49. g = h & 0xf0000000;
  50. if (g)
  51. h ^= g >> 24;
  52. h &= ~g;
  53. }
  54. return h;
  55. }
  56. /* rebuild hash table of section s */
  57. /* NOTE: we do factorize the hash table code to go faster */
  58. static void rebuild_hash(Section *s, unsigned int nb_buckets)
  59. {
  60. ElfW(Sym) *sym;
  61. int *ptr, *hash, nb_syms, sym_index, h;
  62. char *strtab;
  63. strtab = s->link->data;
  64. nb_syms = s->data_offset / sizeof(ElfW(Sym));
  65. s->hash->data_offset = 0;
  66. ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
  67. ptr[0] = nb_buckets;
  68. ptr[1] = nb_syms;
  69. ptr += 2;
  70. hash = ptr;
  71. memset(hash, 0, (nb_buckets + 1) * sizeof(int));
  72. ptr += nb_buckets + 1;
  73. sym = (ElfW(Sym) *)s->data + 1;
  74. for(sym_index = 1; sym_index < nb_syms; sym_index++) {
  75. if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
  76. h = elf_hash(strtab + sym->st_name) % nb_buckets;
  77. *ptr = hash[h];
  78. hash[h] = sym_index;
  79. } else {
  80. *ptr = 0;
  81. }
  82. ptr++;
  83. sym++;
  84. }
  85. }
  86. /* return the symbol number */
  87. static int put_elf_sym(Section *s,
  88. unsigned long value, unsigned long size,
  89. int info, int other, int shndx, const char *name)
  90. {
  91. int name_offset, sym_index;
  92. int nbuckets, h;
  93. ElfW(Sym) *sym;
  94. Section *hs;
  95. sym = section_ptr_add(s, sizeof(ElfW(Sym)));
  96. if (name)
  97. name_offset = put_elf_str(s->link, name);
  98. else
  99. name_offset = 0;
  100. /* XXX: endianness */
  101. sym->st_name = name_offset;
  102. sym->st_value = value;
  103. sym->st_size = size;
  104. sym->st_info = info;
  105. sym->st_other = other;
  106. sym->st_shndx = shndx;
  107. sym_index = sym - (ElfW(Sym) *)s->data;
  108. hs = s->hash;
  109. if (hs) {
  110. int *ptr, *base;
  111. ptr = section_ptr_add(hs, sizeof(int));
  112. base = (int *)hs->data;
  113. /* only add global or weak symbols */
  114. if (ELFW(ST_BIND)(info) != STB_LOCAL) {
  115. /* add another hashing entry */
  116. nbuckets = base[0];
  117. h = elf_hash(name) % nbuckets;
  118. *ptr = base[2 + h];
  119. base[2 + h] = sym_index;
  120. base[1]++;
  121. /* we resize the hash table */
  122. hs->nb_hashed_syms++;
  123. if (hs->nb_hashed_syms > 2 * nbuckets) {
  124. rebuild_hash(s, 2 * nbuckets);
  125. }
  126. } else {
  127. *ptr = 0;
  128. base[1]++;
  129. }
  130. }
  131. return sym_index;
  132. }
  133. /* find global ELF symbol 'name' and return its index. Return 0 if not
  134. found. */
  135. static int find_elf_sym(Section *s, const char *name)
  136. {
  137. ElfW(Sym) *sym;
  138. Section *hs;
  139. int nbuckets, sym_index, h;
  140. const char *name1;
  141. hs = s->hash;
  142. if (!hs)
  143. return 0;
  144. nbuckets = ((int *)hs->data)[0];
  145. h = elf_hash(name) % nbuckets;
  146. sym_index = ((int *)hs->data)[2 + h];
  147. while (sym_index != 0) {
  148. sym = &((ElfW(Sym) *)s->data)[sym_index];
  149. name1 = s->link->data + sym->st_name;
  150. if (!strcmp(name, name1))
  151. return sym_index;
  152. sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
  153. }
  154. return 0;
  155. }
  156. /* return elf symbol value or error */
  157. void *tcc_get_symbol(TCCState *s, const char *name)
  158. {
  159. int sym_index;
  160. ElfW(Sym) *sym;
  161. sym_index = find_elf_sym(symtab_section, name);
  162. if (!sym_index)
  163. return NULL;
  164. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  165. return (void*)(long)sym->st_value;
  166. }
  167. void *tcc_get_symbol_err(TCCState *s, const char *name)
  168. {
  169. void *sym;
  170. sym = tcc_get_symbol(s, name);
  171. if (!sym)
  172. error("%s not defined", name);
  173. return sym;
  174. }
  175. /* add an elf symbol : check if it is already defined and patch
  176. it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
  177. static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
  178. int info, int other, int sh_num, const char *name)
  179. {
  180. ElfW(Sym) *esym;
  181. int sym_bind, sym_index, sym_type, esym_bind;
  182. unsigned char sym_vis, esym_vis, new_vis;
  183. sym_bind = ELFW(ST_BIND)(info);
  184. sym_type = ELFW(ST_TYPE)(info);
  185. sym_vis = ELFW(ST_VISIBILITY)(other);
  186. if (sym_bind != STB_LOCAL) {
  187. /* we search global or weak symbols */
  188. sym_index = find_elf_sym(s, name);
  189. if (!sym_index)
  190. goto do_def;
  191. esym = &((ElfW(Sym) *)s->data)[sym_index];
  192. if (esym->st_shndx != SHN_UNDEF) {
  193. esym_bind = ELFW(ST_BIND)(esym->st_info);
  194. /* propagate the most constraining visibility */
  195. /* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
  196. esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
  197. if (esym_vis == STV_DEFAULT) {
  198. new_vis = sym_vis;
  199. } else if (sym_vis == STV_DEFAULT) {
  200. new_vis = esym_vis;
  201. } else {
  202. new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
  203. }
  204. esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
  205. | new_vis;
  206. other = esym->st_other; /* in case we have to patch esym */
  207. if (sh_num == SHN_UNDEF) {
  208. /* ignore adding of undefined symbol if the
  209. corresponding symbol is already defined */
  210. } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
  211. /* global overrides weak, so patch */
  212. goto do_patch;
  213. } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
  214. /* weak is ignored if already global */
  215. } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
  216. /* ignore hidden symbols after */
  217. } else if (esym->st_shndx == SHN_COMMON && sh_num < SHN_LORESERVE) {
  218. /* gr: Happens with 'tcc ... -static tcctest.c' on e.g. Ubuntu 6.01
  219. No idea if this is the correct solution ... */
  220. goto do_patch;
  221. } else if (s == tcc_state->dynsymtab_section) {
  222. /* we accept that two DLL define the same symbol */
  223. } else {
  224. #if 1
  225. printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
  226. sym_bind, sh_num, new_vis, esym_bind, esym->st_shndx, esym_vis);
  227. #endif
  228. error_noabort("'%s' defined twice", name);
  229. }
  230. } else {
  231. do_patch:
  232. esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
  233. esym->st_shndx = sh_num;
  234. esym->st_value = value;
  235. esym->st_size = size;
  236. esym->st_other = other;
  237. }
  238. } else {
  239. do_def:
  240. sym_index = put_elf_sym(s, value, size,
  241. ELFW(ST_INFO)(sym_bind, sym_type), other,
  242. sh_num, name);
  243. }
  244. return sym_index;
  245. }
  246. /* put relocation */
  247. static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
  248. int type, int symbol)
  249. {
  250. char buf[256];
  251. Section *sr;
  252. ElfW_Rel *rel;
  253. sr = s->reloc;
  254. if (!sr) {
  255. /* if no relocation section, create it */
  256. snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
  257. /* if the symtab is allocated, then we consider the relocation
  258. are also */
  259. sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags);
  260. sr->sh_entsize = sizeof(ElfW_Rel);
  261. sr->link = symtab;
  262. sr->sh_info = s->sh_num;
  263. s->reloc = sr;
  264. }
  265. rel = section_ptr_add(sr, sizeof(ElfW_Rel));
  266. rel->r_offset = offset;
  267. rel->r_info = ELFW(R_INFO)(symbol, type);
  268. #ifdef TCC_TARGET_X86_64
  269. rel->r_addend = 0;
  270. #endif
  271. }
  272. /* put stab debug information */
  273. typedef struct {
  274. unsigned int n_strx; /* index into string table of name */
  275. unsigned char n_type; /* type of symbol */
  276. unsigned char n_other; /* misc info (usually empty) */
  277. unsigned short n_desc; /* description field */
  278. unsigned int n_value; /* value of symbol */
  279. } Stab_Sym;
  280. static void put_stabs(const char *str, int type, int other, int desc,
  281. unsigned long value)
  282. {
  283. Stab_Sym *sym;
  284. sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
  285. if (str) {
  286. sym->n_strx = put_elf_str(stabstr_section, str);
  287. } else {
  288. sym->n_strx = 0;
  289. }
  290. sym->n_type = type;
  291. sym->n_other = other;
  292. sym->n_desc = desc;
  293. sym->n_value = value;
  294. }
  295. static void put_stabs_r(const char *str, int type, int other, int desc,
  296. unsigned long value, Section *sec, int sym_index)
  297. {
  298. put_stabs(str, type, other, desc, value);
  299. put_elf_reloc(symtab_section, stab_section,
  300. stab_section->data_offset - sizeof(unsigned int),
  301. R_DATA_32, sym_index);
  302. }
  303. static void put_stabn(int type, int other, int desc, int value)
  304. {
  305. put_stabs(NULL, type, other, desc, value);
  306. }
  307. static void put_stabd(int type, int other, int desc)
  308. {
  309. put_stabs(NULL, type, other, desc, 0);
  310. }
  311. /* In an ELF file symbol table, the local symbols must appear below
  312. the global and weak ones. Since TCC cannot sort it while generating
  313. the code, we must do it after. All the relocation tables are also
  314. modified to take into account the symbol table sorting */
  315. static void sort_syms(TCCState *s1, Section *s)
  316. {
  317. int *old_to_new_syms;
  318. ElfW(Sym) *new_syms;
  319. int nb_syms, i;
  320. ElfW(Sym) *p, *q;
  321. ElfW_Rel *rel, *rel_end;
  322. Section *sr;
  323. int type, sym_index;
  324. nb_syms = s->data_offset / sizeof(ElfW(Sym));
  325. new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
  326. old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
  327. /* first pass for local symbols */
  328. p = (ElfW(Sym) *)s->data;
  329. q = new_syms;
  330. for(i = 0; i < nb_syms; i++) {
  331. if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
  332. old_to_new_syms[i] = q - new_syms;
  333. *q++ = *p;
  334. }
  335. p++;
  336. }
  337. /* save the number of local symbols in section header */
  338. s->sh_info = q - new_syms;
  339. /* then second pass for non local symbols */
  340. p = (ElfW(Sym) *)s->data;
  341. for(i = 0; i < nb_syms; i++) {
  342. if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
  343. old_to_new_syms[i] = q - new_syms;
  344. *q++ = *p;
  345. }
  346. p++;
  347. }
  348. /* we copy the new symbols to the old */
  349. memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
  350. tcc_free(new_syms);
  351. /* now we modify all the relocations */
  352. for(i = 1; i < s1->nb_sections; i++) {
  353. sr = s1->sections[i];
  354. if (sr->sh_type == SHT_RELX && sr->link == s) {
  355. rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
  356. for(rel = (ElfW_Rel *)sr->data;
  357. rel < rel_end;
  358. rel++) {
  359. sym_index = ELFW(R_SYM)(rel->r_info);
  360. type = ELFW(R_TYPE)(rel->r_info);
  361. sym_index = old_to_new_syms[sym_index];
  362. rel->r_info = ELFW(R_INFO)(sym_index, type);
  363. }
  364. }
  365. }
  366. tcc_free(old_to_new_syms);
  367. }
  368. /* relocate common symbols in the .bss section */
  369. static void relocate_common_syms(void)
  370. {
  371. ElfW(Sym) *sym, *sym_end;
  372. unsigned long offset, align;
  373. sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
  374. for(sym = (ElfW(Sym) *)symtab_section->data + 1;
  375. sym < sym_end;
  376. sym++) {
  377. if (sym->st_shndx == SHN_COMMON) {
  378. /* align symbol */
  379. align = sym->st_value;
  380. offset = bss_section->data_offset;
  381. offset = (offset + align - 1) & -align;
  382. sym->st_value = offset;
  383. sym->st_shndx = bss_section->sh_num;
  384. offset += sym->st_size;
  385. bss_section->data_offset = offset;
  386. }
  387. }
  388. }
  389. /* relocate symbol table, resolve undefined symbols if do_resolve is
  390. true and output error if undefined symbol. */
  391. static void relocate_syms(TCCState *s1, int do_resolve)
  392. {
  393. ElfW(Sym) *sym, *esym, *sym_end;
  394. int sym_bind, sh_num, sym_index;
  395. const char *name;
  396. unsigned long addr;
  397. sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
  398. for(sym = (ElfW(Sym) *)symtab_section->data + 1;
  399. sym < sym_end;
  400. sym++) {
  401. sh_num = sym->st_shndx;
  402. if (sh_num == SHN_UNDEF) {
  403. name = strtab_section->data + sym->st_name;
  404. if (do_resolve) {
  405. name = symtab_section->link->data + sym->st_name;
  406. addr = (unsigned long)resolve_sym(s1, name, ELFW(ST_TYPE)(sym->st_info));
  407. if (addr) {
  408. sym->st_value = addr;
  409. goto found;
  410. }
  411. } else if (s1->dynsym) {
  412. /* if dynamic symbol exist, then use it */
  413. sym_index = find_elf_sym(s1->dynsym, name);
  414. if (sym_index) {
  415. esym = &((ElfW(Sym) *)s1->dynsym->data)[sym_index];
  416. sym->st_value = esym->st_value;
  417. goto found;
  418. }
  419. }
  420. /* XXX: _fp_hw seems to be part of the ABI, so we ignore
  421. it */
  422. if (!strcmp(name, "_fp_hw"))
  423. goto found;
  424. /* only weak symbols are accepted to be undefined. Their
  425. value is zero */
  426. sym_bind = ELFW(ST_BIND)(sym->st_info);
  427. if (sym_bind == STB_WEAK) {
  428. sym->st_value = 0;
  429. } else {
  430. error_noabort("undefined symbol '%s'", name);
  431. }
  432. } else if (sh_num < SHN_LORESERVE) {
  433. /* add section base */
  434. sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
  435. }
  436. found: ;
  437. }
  438. }
  439. #ifdef TCC_TARGET_X86_64
  440. #define JMP_TABLE_ENTRY_SIZE 14
  441. static unsigned long add_jmp_table(TCCState *s1, unsigned long val)
  442. {
  443. char *p = s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset;
  444. s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE;
  445. /* jmp *0x0(%rip) */
  446. p[0] = 0xff;
  447. p[1] = 0x25;
  448. *(int *)(p + 2) = 0;
  449. *(unsigned long *)(p + 6) = val;
  450. return (unsigned long)p;
  451. }
  452. static unsigned long add_got_table(TCCState *s1, unsigned long val)
  453. {
  454. unsigned long *p =(unsigned long *)(s1->runtime_plt_and_got +
  455. s1->runtime_plt_and_got_offset);
  456. s1->runtime_plt_and_got_offset += sizeof(void *);
  457. *p = val;
  458. return (unsigned long)p;
  459. }
  460. #endif
  461. /* relocate a given section (CPU dependent) */
  462. static void relocate_section(TCCState *s1, Section *s)
  463. {
  464. Section *sr;
  465. ElfW_Rel *rel, *rel_end, *qrel;
  466. ElfW(Sym) *sym;
  467. int type, sym_index;
  468. unsigned char *ptr;
  469. unsigned long val, addr;
  470. #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
  471. int esym_index;
  472. #endif
  473. sr = s->reloc;
  474. rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
  475. qrel = (ElfW_Rel *)sr->data;
  476. for(rel = qrel;
  477. rel < rel_end;
  478. rel++) {
  479. ptr = s->data + rel->r_offset;
  480. sym_index = ELFW(R_SYM)(rel->r_info);
  481. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  482. val = sym->st_value;
  483. #ifdef TCC_TARGET_X86_64
  484. /* XXX: not tested */
  485. val += rel->r_addend;
  486. #endif
  487. type = ELFW(R_TYPE)(rel->r_info);
  488. addr = s->sh_addr + rel->r_offset;
  489. /* CPU specific */
  490. switch(type) {
  491. #if defined(TCC_TARGET_I386)
  492. case R_386_32:
  493. if (s1->output_type == TCC_OUTPUT_DLL) {
  494. esym_index = s1->symtab_to_dynsym[sym_index];
  495. qrel->r_offset = rel->r_offset;
  496. if (esym_index) {
  497. qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
  498. qrel++;
  499. break;
  500. } else {
  501. qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
  502. qrel++;
  503. }
  504. }
  505. *(int *)ptr += val;
  506. break;
  507. case R_386_PC32:
  508. if (s1->output_type == TCC_OUTPUT_DLL) {
  509. /* DLL relocation */
  510. esym_index = s1->symtab_to_dynsym[sym_index];
  511. if (esym_index) {
  512. qrel->r_offset = rel->r_offset;
  513. qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
  514. qrel++;
  515. break;
  516. }
  517. }
  518. *(int *)ptr += val - addr;
  519. break;
  520. case R_386_PLT32:
  521. *(int *)ptr += val - addr;
  522. break;
  523. case R_386_GLOB_DAT:
  524. case R_386_JMP_SLOT:
  525. *(int *)ptr = val;
  526. break;
  527. case R_386_GOTPC:
  528. *(int *)ptr += s1->got->sh_addr - addr;
  529. break;
  530. case R_386_GOTOFF:
  531. *(int *)ptr += val - s1->got->sh_addr;
  532. break;
  533. case R_386_GOT32:
  534. /* we load the got offset */
  535. *(int *)ptr += s1->got_offsets[sym_index];
  536. break;
  537. #elif defined(TCC_TARGET_ARM)
  538. case R_ARM_PC24:
  539. case R_ARM_CALL:
  540. case R_ARM_JUMP24:
  541. case R_ARM_PLT32:
  542. {
  543. int x;
  544. x = (*(int *)ptr)&0xffffff;
  545. (*(int *)ptr) &= 0xff000000;
  546. if (x & 0x800000)
  547. x -= 0x1000000;
  548. x *= 4;
  549. x += val - addr;
  550. if((x & 3) != 0 || x >= 0x4000000 || x < -0x4000000)
  551. error("can't relocate value at %x",addr);
  552. x >>= 2;
  553. x &= 0xffffff;
  554. (*(int *)ptr) |= x;
  555. }
  556. break;
  557. case R_ARM_PREL31:
  558. {
  559. int x;
  560. x = (*(int *)ptr) & 0x7fffffff;
  561. (*(int *)ptr) &= 0x80000000;
  562. x = (x * 2) / 2;
  563. x += val - addr;
  564. if((x^(x>>1))&0x40000000)
  565. error("can't relocate value at %x",addr);
  566. (*(int *)ptr) |= x & 0x7fffffff;
  567. }
  568. case R_ARM_ABS32:
  569. *(int *)ptr += val;
  570. break;
  571. case R_ARM_BASE_PREL:
  572. *(int *)ptr += s1->got->sh_addr - addr;
  573. break;
  574. case R_ARM_GOTOFF32:
  575. *(int *)ptr += val - s1->got->sh_addr;
  576. break;
  577. case R_ARM_GOT_BREL:
  578. /* we load the got offset */
  579. *(int *)ptr += s1->got_offsets[sym_index];
  580. break;
  581. case R_ARM_COPY:
  582. break;
  583. default:
  584. fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",
  585. type,addr,(unsigned int )ptr,val);
  586. break;
  587. #elif defined(TCC_TARGET_C67)
  588. case R_C60_32:
  589. *(int *)ptr += val;
  590. break;
  591. case R_C60LO16:
  592. {
  593. uint32_t orig;
  594. /* put the low 16 bits of the absolute address */
  595. // add to what is already there
  596. orig = ((*(int *)(ptr )) >> 7) & 0xffff;
  597. orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16;
  598. //patch both at once - assumes always in pairs Low - High
  599. *(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | (((val+orig) & 0xffff) << 7);
  600. *(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | ((((val+orig)>>16) & 0xffff) << 7);
  601. }
  602. break;
  603. case R_C60HI16:
  604. break;
  605. default:
  606. fprintf(stderr,"FIXME: handle reloc type %x at %lx [%.8x] to %lx\n",
  607. type,addr,(unsigned int )ptr,val);
  608. break;
  609. #elif defined(TCC_TARGET_X86_64)
  610. case R_X86_64_64:
  611. if (s1->output_type == TCC_OUTPUT_DLL) {
  612. qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
  613. qrel->r_addend = *(long long *)ptr + val;
  614. qrel++;
  615. }
  616. *(long long *)ptr += val;
  617. break;
  618. case R_X86_64_32:
  619. case R_X86_64_32S:
  620. if (s1->output_type == TCC_OUTPUT_DLL) {
  621. /* XXX: this logic may depend on TCC's codegen
  622. now TCC uses R_X86_64_32 even for a 64bit pointer */
  623. qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
  624. qrel->r_addend = *(int *)ptr + val;
  625. qrel++;
  626. }
  627. *(int *)ptr += val;
  628. break;
  629. case R_X86_64_PC32: {
  630. if (s1->output_type == TCC_OUTPUT_DLL) {
  631. /* DLL relocation */
  632. esym_index = s1->symtab_to_dynsym[sym_index];
  633. if (esym_index) {
  634. qrel->r_offset = rel->r_offset;
  635. qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
  636. qrel->r_addend = *(int *)ptr;
  637. qrel++;
  638. break;
  639. }
  640. }
  641. long diff = val - addr;
  642. if (diff <= -2147483647 || diff > 2147483647) {
  643. /* XXX: naive support for over 32bit jump */
  644. if (s1->output_type == TCC_OUTPUT_MEMORY) {
  645. val = add_jmp_table(s1, val);
  646. diff = val - addr;
  647. }
  648. if (diff <= -2147483647 || diff > 2147483647) {
  649. error("internal error: relocation failed");
  650. }
  651. }
  652. *(int *)ptr += diff;
  653. }
  654. break;
  655. case R_X86_64_PLT32:
  656. *(int *)ptr += val - addr;
  657. break;
  658. case R_X86_64_GLOB_DAT:
  659. case R_X86_64_JUMP_SLOT:
  660. *(int *)ptr = val;
  661. break;
  662. case R_X86_64_GOTPCREL:
  663. if (s1->output_type == TCC_OUTPUT_MEMORY) {
  664. val = add_got_table(s1, val - rel->r_addend) + rel->r_addend;
  665. *(int *)ptr += val - addr;
  666. break;
  667. }
  668. *(int *)ptr += (s1->got->sh_addr - addr +
  669. s1->got_offsets[sym_index] - 4);
  670. break;
  671. case R_X86_64_GOTTPOFF:
  672. *(int *)ptr += val - s1->got->sh_addr;
  673. break;
  674. case R_X86_64_GOT32:
  675. /* we load the got offset */
  676. *(int *)ptr += s1->got_offsets[sym_index];
  677. break;
  678. #else
  679. #error unsupported processor
  680. #endif
  681. }
  682. }
  683. /* if the relocation is allocated, we change its symbol table */
  684. if (sr->sh_flags & SHF_ALLOC)
  685. sr->link = s1->dynsym;
  686. }
  687. /* relocate relocation table in 'sr' */
  688. static void relocate_rel(TCCState *s1, Section *sr)
  689. {
  690. Section *s;
  691. ElfW_Rel *rel, *rel_end;
  692. s = s1->sections[sr->sh_info];
  693. rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
  694. for(rel = (ElfW_Rel *)sr->data;
  695. rel < rel_end;
  696. rel++) {
  697. rel->r_offset += s->sh_addr;
  698. }
  699. }
  700. /* count the number of dynamic relocations so that we can reserve
  701. their space */
  702. static int prepare_dynamic_rel(TCCState *s1, Section *sr)
  703. {
  704. ElfW_Rel *rel, *rel_end;
  705. int sym_index, esym_index, type, count;
  706. count = 0;
  707. rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
  708. for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
  709. sym_index = ELFW(R_SYM)(rel->r_info);
  710. type = ELFW(R_TYPE)(rel->r_info);
  711. switch(type) {
  712. #if defined(TCC_TARGET_I386)
  713. case R_386_32:
  714. #elif defined(TCC_TARGET_X86_64)
  715. case R_X86_64_32:
  716. case R_X86_64_32S:
  717. case R_X86_64_64:
  718. #endif
  719. count++;
  720. break;
  721. #if defined(TCC_TARGET_I386)
  722. case R_386_PC32:
  723. #elif defined(TCC_TARGET_X86_64)
  724. case R_X86_64_PC32:
  725. #endif
  726. esym_index = s1->symtab_to_dynsym[sym_index];
  727. if (esym_index)
  728. count++;
  729. break;
  730. default:
  731. break;
  732. }
  733. }
  734. if (count) {
  735. /* allocate the section */
  736. sr->sh_flags |= SHF_ALLOC;
  737. sr->sh_size = count * sizeof(ElfW_Rel);
  738. }
  739. return count;
  740. }
  741. static void put_got_offset(TCCState *s1, int index, unsigned long val)
  742. {
  743. int n;
  744. unsigned long *tab;
  745. if (index >= s1->nb_got_offsets) {
  746. /* find immediately bigger power of 2 and reallocate array */
  747. n = 1;
  748. while (index >= n)
  749. n *= 2;
  750. tab = tcc_realloc(s1->got_offsets, n * sizeof(unsigned long));
  751. if (!tab)
  752. error("memory full");
  753. s1->got_offsets = tab;
  754. memset(s1->got_offsets + s1->nb_got_offsets, 0,
  755. (n - s1->nb_got_offsets) * sizeof(unsigned long));
  756. s1->nb_got_offsets = n;
  757. }
  758. s1->got_offsets[index] = val;
  759. }
  760. /* XXX: suppress that */
  761. static void put32(unsigned char *p, uint32_t val)
  762. {
  763. p[0] = val;
  764. p[1] = val >> 8;
  765. p[2] = val >> 16;
  766. p[3] = val >> 24;
  767. }
  768. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_ARM) || \
  769. defined(TCC_TARGET_X86_64)
  770. static uint32_t get32(unsigned char *p)
  771. {
  772. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  773. }
  774. #endif
  775. static void build_got(TCCState *s1)
  776. {
  777. unsigned char *ptr;
  778. /* if no got, then create it */
  779. s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
  780. s1->got->sh_entsize = 4;
  781. add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
  782. 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
  783. ptr = section_ptr_add(s1->got, 3 * PTR_SIZE);
  784. #if PTR_SIZE == 4
  785. /* keep space for _DYNAMIC pointer, if present */
  786. put32(ptr, 0);
  787. /* two dummy got entries */
  788. put32(ptr + 4, 0);
  789. put32(ptr + 8, 0);
  790. #else
  791. /* keep space for _DYNAMIC pointer, if present */
  792. put32(ptr, 0);
  793. put32(ptr + 4, 0);
  794. /* two dummy got entries */
  795. put32(ptr + 8, 0);
  796. put32(ptr + 12, 0);
  797. put32(ptr + 16, 0);
  798. put32(ptr + 20, 0);
  799. #endif
  800. }
  801. /* put a got entry corresponding to a symbol in symtab_section. 'size'
  802. and 'info' can be modifed if more precise info comes from the DLL */
  803. static void put_got_entry(TCCState *s1,
  804. int reloc_type, unsigned long size, int info,
  805. int sym_index)
  806. {
  807. int index;
  808. const char *name;
  809. ElfW(Sym) *sym;
  810. unsigned long offset;
  811. int *ptr;
  812. if (!s1->got)
  813. build_got(s1);
  814. /* if a got entry already exists for that symbol, no need to add one */
  815. if (sym_index < s1->nb_got_offsets &&
  816. s1->got_offsets[sym_index] != 0)
  817. return;
  818. put_got_offset(s1, sym_index, s1->got->data_offset);
  819. if (s1->dynsym) {
  820. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  821. name = symtab_section->link->data + sym->st_name;
  822. offset = sym->st_value;
  823. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  824. if (reloc_type ==
  825. #ifdef TCC_TARGET_X86_64
  826. R_X86_64_JUMP_SLOT
  827. #else
  828. R_386_JMP_SLOT
  829. #endif
  830. ) {
  831. Section *plt;
  832. uint8_t *p;
  833. int modrm;
  834. #if defined(TCC_OUTPUT_DLL_WITH_PLT)
  835. modrm = 0x25;
  836. #else
  837. /* if we build a DLL, we add a %ebx offset */
  838. if (s1->output_type == TCC_OUTPUT_DLL)
  839. modrm = 0xa3;
  840. else
  841. modrm = 0x25;
  842. #endif
  843. /* add a PLT entry */
  844. plt = s1->plt;
  845. if (plt->data_offset == 0) {
  846. /* first plt entry */
  847. p = section_ptr_add(plt, 16);
  848. p[0] = 0xff; /* pushl got + PTR_SIZE */
  849. p[1] = modrm + 0x10;
  850. put32(p + 2, PTR_SIZE);
  851. p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
  852. p[7] = modrm;
  853. put32(p + 8, PTR_SIZE * 2);
  854. }
  855. p = section_ptr_add(plt, 16);
  856. p[0] = 0xff; /* jmp *(got + x) */
  857. p[1] = modrm;
  858. put32(p + 2, s1->got->data_offset);
  859. p[6] = 0x68; /* push $xxx */
  860. put32(p + 7, (plt->data_offset - 32) >> 1);
  861. p[11] = 0xe9; /* jmp plt_start */
  862. put32(p + 12, -(plt->data_offset));
  863. /* the symbol is modified so that it will be relocated to
  864. the PLT */
  865. #if !defined(TCC_OUTPUT_DLL_WITH_PLT)
  866. if (s1->output_type == TCC_OUTPUT_EXE)
  867. #endif
  868. offset = plt->data_offset - 16;
  869. }
  870. #elif defined(TCC_TARGET_ARM)
  871. if (reloc_type == R_ARM_JUMP_SLOT) {
  872. Section *plt;
  873. uint8_t *p;
  874. /* if we build a DLL, we add a %ebx offset */
  875. if (s1->output_type == TCC_OUTPUT_DLL)
  876. error("DLLs unimplemented!");
  877. /* add a PLT entry */
  878. plt = s1->plt;
  879. if (plt->data_offset == 0) {
  880. /* first plt entry */
  881. p = section_ptr_add(plt, 16);
  882. put32(p , 0xe52de004);
  883. put32(p + 4, 0xe59fe010);
  884. put32(p + 8, 0xe08fe00e);
  885. put32(p + 12, 0xe5bef008);
  886. }
  887. p = section_ptr_add(plt, 16);
  888. put32(p , 0xe59fc004);
  889. put32(p+4, 0xe08fc00c);
  890. put32(p+8, 0xe59cf000);
  891. put32(p+12, s1->got->data_offset);
  892. /* the symbol is modified so that it will be relocated to
  893. the PLT */
  894. if (s1->output_type == TCC_OUTPUT_EXE)
  895. offset = plt->data_offset - 16;
  896. }
  897. #elif defined(TCC_TARGET_C67)
  898. error("C67 got not implemented");
  899. #else
  900. #error unsupported CPU
  901. #endif
  902. index = put_elf_sym(s1->dynsym, offset,
  903. size, info, 0, sym->st_shndx, name);
  904. /* put a got entry */
  905. put_elf_reloc(s1->dynsym, s1->got,
  906. s1->got->data_offset,
  907. reloc_type, index);
  908. }
  909. ptr = section_ptr_add(s1->got, PTR_SIZE);
  910. *ptr = 0;
  911. }
  912. /* build GOT and PLT entries */
  913. static void build_got_entries(TCCState *s1)
  914. {
  915. Section *s, *symtab;
  916. ElfW_Rel *rel, *rel_end;
  917. ElfW(Sym) *sym;
  918. int i, type, reloc_type, sym_index;
  919. for(i = 1; i < s1->nb_sections; i++) {
  920. s = s1->sections[i];
  921. if (s->sh_type != SHT_RELX)
  922. continue;
  923. /* no need to handle got relocations */
  924. if (s->link != symtab_section)
  925. continue;
  926. symtab = s->link;
  927. rel_end = (ElfW_Rel *)(s->data + s->data_offset);
  928. for(rel = (ElfW_Rel *)s->data;
  929. rel < rel_end;
  930. rel++) {
  931. type = ELFW(R_TYPE)(rel->r_info);
  932. switch(type) {
  933. #if defined(TCC_TARGET_I386)
  934. case R_386_GOT32:
  935. case R_386_GOTOFF:
  936. case R_386_GOTPC:
  937. case R_386_PLT32:
  938. if (!s1->got)
  939. build_got(s1);
  940. if (type == R_386_GOT32 || type == R_386_PLT32) {
  941. sym_index = ELFW(R_SYM)(rel->r_info);
  942. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  943. /* look at the symbol got offset. If none, then add one */
  944. if (type == R_386_GOT32)
  945. reloc_type = R_386_GLOB_DAT;
  946. else
  947. reloc_type = R_386_JMP_SLOT;
  948. put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
  949. sym_index);
  950. }
  951. break;
  952. #elif defined(TCC_TARGET_ARM)
  953. case R_ARM_GOT_BREL:
  954. case R_ARM_GOTOFF32:
  955. case R_ARM_BASE_PREL:
  956. case R_ARM_PLT32:
  957. if (!s1->got)
  958. build_got(s1);
  959. if (type == R_ARM_GOT_BREL || type == R_ARM_PLT32) {
  960. sym_index = ELFW(R_SYM)(rel->r_info);
  961. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  962. /* look at the symbol got offset. If none, then add one */
  963. if (type == R_ARM_GOT_BREL)
  964. reloc_type = R_ARM_GLOB_DAT;
  965. else
  966. reloc_type = R_ARM_JUMP_SLOT;
  967. put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
  968. sym_index);
  969. }
  970. break;
  971. #elif defined(TCC_TARGET_C67)
  972. case R_C60_GOT32:
  973. case R_C60_GOTOFF:
  974. case R_C60_GOTPC:
  975. case R_C60_PLT32:
  976. if (!s1->got)
  977. build_got(s1);
  978. if (type == R_C60_GOT32 || type == R_C60_PLT32) {
  979. sym_index = ELFW(R_SYM)(rel->r_info);
  980. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  981. /* look at the symbol got offset. If none, then add one */
  982. if (type == R_C60_GOT32)
  983. reloc_type = R_C60_GLOB_DAT;
  984. else
  985. reloc_type = R_C60_JMP_SLOT;
  986. put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
  987. sym_index);
  988. }
  989. break;
  990. #elif defined(TCC_TARGET_X86_64)
  991. case R_X86_64_GOT32:
  992. case R_X86_64_GOTTPOFF:
  993. case R_X86_64_GOTPCREL:
  994. case R_X86_64_PLT32:
  995. if (!s1->got)
  996. build_got(s1);
  997. if (type == R_X86_64_GOT32 || type == R_X86_64_GOTPCREL ||
  998. type == R_X86_64_PLT32) {
  999. sym_index = ELFW(R_SYM)(rel->r_info);
  1000. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  1001. /* look at the symbol got offset. If none, then add one */
  1002. if (type == R_X86_64_GOT32 || type == R_X86_64_GOTPCREL)
  1003. reloc_type = R_X86_64_GLOB_DAT;
  1004. else
  1005. reloc_type = R_X86_64_JUMP_SLOT;
  1006. put_got_entry(s1, reloc_type, sym->st_size, sym->st_info,
  1007. sym_index);
  1008. }
  1009. break;
  1010. #else
  1011. #error unsupported CPU
  1012. #endif
  1013. default:
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. static Section *new_symtab(TCCState *s1,
  1020. const char *symtab_name, int sh_type, int sh_flags,
  1021. const char *strtab_name,
  1022. const char *hash_name, int hash_sh_flags)
  1023. {
  1024. Section *symtab, *strtab, *hash;
  1025. int *ptr, nb_buckets;
  1026. symtab = new_section(s1, symtab_name, sh_type, sh_flags);
  1027. symtab->sh_entsize = sizeof(ElfW(Sym));
  1028. strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
  1029. put_elf_str(strtab, "");
  1030. symtab->link = strtab;
  1031. put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL);
  1032. nb_buckets = 1;
  1033. hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
  1034. hash->sh_entsize = sizeof(int);
  1035. symtab->hash = hash;
  1036. hash->link = symtab;
  1037. ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int));
  1038. ptr[0] = nb_buckets;
  1039. ptr[1] = 1;
  1040. memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
  1041. return symtab;
  1042. }
  1043. /* put dynamic tag */
  1044. static void put_dt(Section *dynamic, int dt, unsigned long val)
  1045. {
  1046. ElfW(Dyn) *dyn;
  1047. dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));
  1048. dyn->d_tag = dt;
  1049. dyn->d_un.d_val = val;
  1050. }
  1051. static void add_init_array_defines(TCCState *s1, const char *section_name)
  1052. {
  1053. Section *s;
  1054. long end_offset;
  1055. char sym_start[1024];
  1056. char sym_end[1024];
  1057. snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1);
  1058. snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1);
  1059. s = find_section(s1, section_name);
  1060. if (!s) {
  1061. end_offset = 0;
  1062. s = data_section;
  1063. } else {
  1064. end_offset = s->data_offset;
  1065. }
  1066. add_elf_sym(symtab_section,
  1067. 0, 0,
  1068. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1069. s->sh_num, sym_start);
  1070. add_elf_sym(symtab_section,
  1071. end_offset, 0,
  1072. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1073. s->sh_num, sym_end);
  1074. }
  1075. /* add tcc runtime libraries */
  1076. static void tcc_add_runtime(TCCState *s1)
  1077. {
  1078. #if defined(CONFIG_TCC_BCHECK) || !defined(CONFIG_USE_LIBGCC)
  1079. char buf[1024];
  1080. #endif
  1081. #ifdef CONFIG_TCC_BCHECK
  1082. if (s1->do_bounds_check) {
  1083. unsigned long *ptr;
  1084. Section *init_section;
  1085. unsigned char *pinit;
  1086. int sym_index;
  1087. /* XXX: add an object file to do that */
  1088. ptr = section_ptr_add(bounds_section, sizeof(unsigned long));
  1089. *ptr = 0;
  1090. add_elf_sym(symtab_section, 0, 0,
  1091. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1092. bounds_section->sh_num, "__bounds_start");
  1093. /* add bound check code */
  1094. snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, "bcheck.o");
  1095. tcc_add_file(s1, buf);
  1096. #ifdef TCC_TARGET_I386
  1097. if (s1->output_type != TCC_OUTPUT_MEMORY) {
  1098. /* add 'call __bound_init()' in .init section */
  1099. init_section = find_section(s1, ".init");
  1100. pinit = section_ptr_add(init_section, 5);
  1101. pinit[0] = 0xe8;
  1102. put32(pinit + 1, -4);
  1103. sym_index = find_elf_sym(symtab_section, "__bound_init");
  1104. put_elf_reloc(symtab_section, init_section,
  1105. init_section->data_offset - 4, R_386_PC32, sym_index);
  1106. }
  1107. #endif
  1108. }
  1109. #endif
  1110. /* add libc */
  1111. if (!s1->nostdlib) {
  1112. tcc_add_library(s1, "c");
  1113. #ifdef CONFIG_USE_LIBGCC
  1114. tcc_add_file(s1, CONFIG_SYSROOT "/lib/libgcc_s.so.1");
  1115. #else
  1116. snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, "libtcc1.a");
  1117. tcc_add_file(s1, buf);
  1118. #endif
  1119. }
  1120. /* add crt end if not memory output */
  1121. if (s1->output_type != TCC_OUTPUT_MEMORY && !s1->nostdlib) {
  1122. tcc_add_file(s1, CONFIG_TCC_CRT_PREFIX "/crtn.o");
  1123. }
  1124. }
  1125. /* add various standard linker symbols (must be done after the
  1126. sections are filled (for example after allocating common
  1127. symbols)) */
  1128. static void tcc_add_linker_symbols(TCCState *s1)
  1129. {
  1130. char buf[1024];
  1131. int i;
  1132. Section *s;
  1133. add_elf_sym(symtab_section,
  1134. text_section->data_offset, 0,
  1135. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1136. text_section->sh_num, "_etext");
  1137. add_elf_sym(symtab_section,
  1138. data_section->data_offset, 0,
  1139. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1140. data_section->sh_num, "_edata");
  1141. add_elf_sym(symtab_section,
  1142. bss_section->data_offset, 0,
  1143. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1144. bss_section->sh_num, "_end");
  1145. /* horrible new standard ldscript defines */
  1146. add_init_array_defines(s1, ".preinit_array");
  1147. add_init_array_defines(s1, ".init_array");
  1148. add_init_array_defines(s1, ".fini_array");
  1149. /* add start and stop symbols for sections whose name can be
  1150. expressed in C */
  1151. for(i = 1; i < s1->nb_sections; i++) {
  1152. s = s1->sections[i];
  1153. if (s->sh_type == SHT_PROGBITS &&
  1154. (s->sh_flags & SHF_ALLOC)) {
  1155. const char *p;
  1156. int ch;
  1157. /* check if section name can be expressed in C */
  1158. p = s->name;
  1159. for(;;) {
  1160. ch = *p;
  1161. if (!ch)
  1162. break;
  1163. if (!isid(ch) && !isnum(ch))
  1164. goto next_sec;
  1165. p++;
  1166. }
  1167. snprintf(buf, sizeof(buf), "__start_%s", s->name);
  1168. add_elf_sym(symtab_section,
  1169. 0, 0,
  1170. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1171. s->sh_num, buf);
  1172. snprintf(buf, sizeof(buf), "__stop_%s", s->name);
  1173. add_elf_sym(symtab_section,
  1174. s->data_offset, 0,
  1175. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1176. s->sh_num, buf);
  1177. }
  1178. next_sec: ;
  1179. }
  1180. }
  1181. /* name of ELF interpreter */
  1182. #if defined __FreeBSD__
  1183. static char elf_interp[] = "/usr/libexec/ld-elf.so.1";
  1184. #elif defined TCC_ARM_EABI
  1185. static char elf_interp[] = "/lib/ld-linux.so.3";
  1186. #elif defined(TCC_TARGET_X86_64)
  1187. static char elf_interp[] = "/lib/ld-linux-x86-64.so.2";
  1188. #elif defined(TCC_UCLIBC)
  1189. static char elf_interp[] = "/lib/ld-uClibc.so.0";
  1190. #else
  1191. static char elf_interp[] = "/lib/ld-linux.so.2";
  1192. #endif
  1193. static void tcc_output_binary(TCCState *s1, FILE *f,
  1194. const int *section_order)
  1195. {
  1196. Section *s;
  1197. int i, offset, size;
  1198. offset = 0;
  1199. for(i=1;i<s1->nb_sections;i++) {
  1200. s = s1->sections[section_order[i]];
  1201. if (s->sh_type != SHT_NOBITS &&
  1202. (s->sh_flags & SHF_ALLOC)) {
  1203. while (offset < s->sh_offset) {
  1204. fputc(0, f);
  1205. offset++;
  1206. }
  1207. size = s->sh_size;
  1208. fwrite(s->data, 1, size, f);
  1209. offset += size;
  1210. }
  1211. }
  1212. }
  1213. /* output an ELF file */
  1214. /* XXX: suppress unneeded sections */
  1215. int elf_output_file(TCCState *s1, const char *filename)
  1216. {
  1217. ElfW(Ehdr) ehdr;
  1218. FILE *f;
  1219. int fd, mode, ret;
  1220. int *section_order;
  1221. int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k;
  1222. unsigned long addr;
  1223. Section *strsec, *s;
  1224. ElfW(Shdr) shdr, *sh;
  1225. ElfW(Phdr) *phdr, *ph;
  1226. Section *interp, *dynamic, *dynstr;
  1227. unsigned long saved_dynamic_data_offset;
  1228. ElfW(Sym) *sym;
  1229. int type, file_type;
  1230. unsigned long rel_addr, rel_size;
  1231. file_type = s1->output_type;
  1232. s1->nb_errors = 0;
  1233. if (file_type != TCC_OUTPUT_OBJ) {
  1234. tcc_add_runtime(s1);
  1235. }
  1236. phdr = NULL;
  1237. section_order = NULL;
  1238. interp = NULL;
  1239. dynamic = NULL;
  1240. dynstr = NULL; /* avoid warning */
  1241. saved_dynamic_data_offset = 0; /* avoid warning */
  1242. if (file_type != TCC_OUTPUT_OBJ) {
  1243. relocate_common_syms();
  1244. tcc_add_linker_symbols(s1);
  1245. if (!s1->static_link) {
  1246. const char *name;
  1247. int sym_index, index;
  1248. ElfW(Sym) *esym, *sym_end;
  1249. if (file_type == TCC_OUTPUT_EXE) {
  1250. char *ptr;
  1251. /* add interpreter section only if executable */
  1252. interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
  1253. interp->sh_addralign = 1;
  1254. ptr = section_ptr_add(interp, sizeof(elf_interp));
  1255. strcpy(ptr, elf_interp);
  1256. }
  1257. /* add dynamic symbol table */
  1258. s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
  1259. ".dynstr",
  1260. ".hash", SHF_ALLOC);
  1261. dynstr = s1->dynsym->link;
  1262. /* add dynamic section */
  1263. dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
  1264. SHF_ALLOC | SHF_WRITE);
  1265. dynamic->link = dynstr;
  1266. dynamic->sh_entsize = sizeof(ElfW(Dyn));
  1267. /* add PLT */
  1268. s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
  1269. SHF_ALLOC | SHF_EXECINSTR);
  1270. s1->plt->sh_entsize = 4;
  1271. build_got(s1);
  1272. /* scan for undefined symbols and see if they are in the
  1273. dynamic symbols. If a symbol STT_FUNC is found, then we
  1274. add it in the PLT. If a symbol STT_OBJECT is found, we
  1275. add it in the .bss section with a suitable relocation */
  1276. sym_end = (ElfW(Sym) *)(symtab_section->data +
  1277. symtab_section->data_offset);
  1278. if (file_type == TCC_OUTPUT_EXE) {
  1279. for(sym = (ElfW(Sym) *)symtab_section->data + 1;
  1280. sym < sym_end;
  1281. sym++) {
  1282. if (sym->st_shndx == SHN_UNDEF) {
  1283. name = symtab_section->link->data + sym->st_name;
  1284. sym_index = find_elf_sym(s1->dynsymtab_section, name);
  1285. if (sym_index) {
  1286. esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index];
  1287. type = ELFW(ST_TYPE)(esym->st_info);
  1288. if (type == STT_FUNC) {
  1289. put_got_entry(s1, R_JMP_SLOT, esym->st_size,
  1290. esym->st_info,
  1291. sym - (ElfW(Sym) *)symtab_section->data);
  1292. } else if (type == STT_OBJECT) {
  1293. unsigned long offset;
  1294. offset = bss_section->data_offset;
  1295. /* XXX: which alignment ? */
  1296. offset = (offset + 16 - 1) & -16;
  1297. index = put_elf_sym(s1->dynsym, offset, esym->st_size,
  1298. esym->st_info, 0,
  1299. bss_section->sh_num, name);
  1300. put_elf_reloc(s1->dynsym, bss_section,
  1301. offset, R_COPY, index);
  1302. offset += esym->st_size;
  1303. bss_section->data_offset = offset;
  1304. }
  1305. } else {
  1306. /* STB_WEAK undefined symbols are accepted */
  1307. /* XXX: _fp_hw seems to be part of the ABI, so we ignore
  1308. it */
  1309. if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK ||
  1310. !strcmp(name, "_fp_hw")) {
  1311. } else {
  1312. error_noabort("undefined symbol '%s'", name);
  1313. }
  1314. }
  1315. } else if (s1->rdynamic &&
  1316. ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
  1317. /* if -rdynamic option, then export all non
  1318. local symbols */
  1319. name = symtab_section->link->data + sym->st_name;
  1320. put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
  1321. sym->st_info, 0,
  1322. sym->st_shndx, name);
  1323. }
  1324. }
  1325. if (s1->nb_errors)
  1326. goto fail;
  1327. /* now look at unresolved dynamic symbols and export
  1328. corresponding symbol */
  1329. sym_end = (ElfW(Sym) *)(s1->dynsymtab_section->data +
  1330. s1->dynsymtab_section->data_offset);
  1331. for(esym = (ElfW(Sym) *)s1->dynsymtab_section->data + 1;
  1332. esym < sym_end;
  1333. esym++) {
  1334. if (esym->st_shndx == SHN_UNDEF) {
  1335. name = s1->dynsymtab_section->link->data + esym->st_name;
  1336. sym_index = find_elf_sym(symtab_section, name);
  1337. if (sym_index) {
  1338. /* XXX: avoid adding a symbol if already
  1339. present because of -rdynamic ? */
  1340. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  1341. put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
  1342. sym->st_info, 0,
  1343. sym->st_shndx, name);
  1344. } else {
  1345. if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
  1346. /* weak symbols can stay undefined */
  1347. } else {
  1348. warning("undefined dynamic symbol '%s'", name);
  1349. }
  1350. }
  1351. }
  1352. }
  1353. } else {
  1354. int nb_syms;
  1355. /* shared library case : we simply export all the global symbols */
  1356. nb_syms = symtab_section->data_offset / sizeof(ElfW(Sym));
  1357. s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms);
  1358. for(sym = (ElfW(Sym) *)symtab_section->data + 1;
  1359. sym < sym_end;
  1360. sym++) {
  1361. if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
  1362. #if defined(TCC_OUTPUT_DLL_WITH_PLT)
  1363. if (ELFW(ST_TYPE)(sym->st_info) == STT_FUNC &&
  1364. sym->st_shndx == SHN_UNDEF) {
  1365. put_got_entry(s1, R_JMP_SLOT, sym->st_size,
  1366. sym->st_info,
  1367. sym - (ElfW(Sym) *)symtab_section->data);
  1368. }
  1369. else if (ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT) {
  1370. put_got_entry(s1, R_X86_64_GLOB_DAT, sym->st_size,
  1371. sym->st_info,
  1372. sym - (ElfW(Sym) *)symtab_section->data);
  1373. }
  1374. else
  1375. #endif
  1376. {
  1377. name = symtab_section->link->data + sym->st_name;
  1378. index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size,
  1379. sym->st_info, 0,
  1380. sym->st_shndx, name);
  1381. s1->symtab_to_dynsym[sym -
  1382. (ElfW(Sym) *)symtab_section->data] =
  1383. index;
  1384. }
  1385. }
  1386. }
  1387. }
  1388. build_got_entries(s1);
  1389. /* add a list of needed dlls */
  1390. for(i = 0; i < s1->nb_loaded_dlls; i++) {
  1391. DLLReference *dllref = s1->loaded_dlls[i];
  1392. if (dllref->level == 0)
  1393. put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
  1394. }
  1395. /* XXX: currently, since we do not handle PIC code, we
  1396. must relocate the readonly segments */
  1397. if (file_type == TCC_OUTPUT_DLL) {
  1398. if (s1->soname)
  1399. put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
  1400. put_dt(dynamic, DT_TEXTREL, 0);
  1401. }
  1402. /* add necessary space for other entries */
  1403. saved_dynamic_data_offset = dynamic->data_offset;
  1404. dynamic->data_offset += sizeof(ElfW(Dyn)) * 9;
  1405. } else {
  1406. /* still need to build got entries in case of static link */
  1407. build_got_entries(s1);
  1408. }
  1409. }
  1410. memset(&ehdr, 0, sizeof(ehdr));
  1411. /* we add a section for symbols */
  1412. strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
  1413. put_elf_str(strsec, "");
  1414. /* compute number of sections */
  1415. shnum = s1->nb_sections;
  1416. /* this array is used to reorder sections in the output file */
  1417. section_order = tcc_malloc(sizeof(int) * shnum);
  1418. section_order[0] = 0;
  1419. sh_order_index = 1;
  1420. /* compute number of program headers */
  1421. switch(file_type) {
  1422. default:
  1423. case TCC_OUTPUT_OBJ:
  1424. phnum = 0;
  1425. break;
  1426. case TCC_OUTPUT_EXE:
  1427. if (!s1->static_link)
  1428. phnum = 4;
  1429. else
  1430. phnum = 2;
  1431. break;
  1432. case TCC_OUTPUT_DLL:
  1433. phnum = 3;
  1434. break;
  1435. }
  1436. /* allocate strings for section names and decide if an unallocated
  1437. section should be output */
  1438. /* NOTE: the strsec section comes last, so its size is also
  1439. correct ! */
  1440. for(i = 1; i < s1->nb_sections; i++) {
  1441. s = s1->sections[i];
  1442. s->sh_name = put_elf_str(strsec, s->name);
  1443. #if 0 //gr
  1444. printf("section: f=%08x t=%08x i=%08x %s %s\n",
  1445. s->sh_flags,
  1446. s->sh_type,
  1447. s->sh_info,
  1448. s->name,
  1449. s->reloc ? s->reloc->name : "n"
  1450. );
  1451. #endif
  1452. /* when generating a DLL, we include relocations but we may
  1453. patch them */
  1454. if (file_type == TCC_OUTPUT_DLL &&
  1455. s->sh_type == SHT_RELX &&
  1456. !(s->sh_flags & SHF_ALLOC)) {
  1457. /* //gr: avoid bogus relocs for empty (debug) sections */
  1458. if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC)
  1459. prepare_dynamic_rel(s1, s);
  1460. else if (s1->do_debug)
  1461. s->sh_size = s->data_offset;
  1462. } else if (s1->do_debug ||
  1463. file_type == TCC_OUTPUT_OBJ ||
  1464. (s->sh_flags & SHF_ALLOC) ||
  1465. i == (s1->nb_sections - 1)) {
  1466. /* we output all sections if debug or object file */
  1467. s->sh_size = s->data_offset;
  1468. }
  1469. }
  1470. /* allocate program segment headers */
  1471. phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
  1472. if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
  1473. file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
  1474. } else {
  1475. file_offset = 0;
  1476. }
  1477. if (phnum > 0) {
  1478. /* compute section to program header mapping */
  1479. if (s1->has_text_addr) {
  1480. int a_offset, p_offset;
  1481. addr = s1->text_addr;
  1482. /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset %
  1483. ELF_PAGE_SIZE */
  1484. a_offset = addr & (ELF_PAGE_SIZE - 1);
  1485. p_offset = file_offset & (ELF_PAGE_SIZE - 1);
  1486. if (a_offset < p_offset)
  1487. a_offset += ELF_PAGE_SIZE;
  1488. file_offset += (a_offset - p_offset);
  1489. } else {
  1490. if (file_type == TCC_OUTPUT_DLL)
  1491. addr = 0;
  1492. else
  1493. addr = ELF_START_ADDR;
  1494. /* compute address after headers */
  1495. addr += (file_offset & (ELF_PAGE_SIZE - 1));
  1496. }
  1497. /* dynamic relocation table information, for .dynamic section */
  1498. rel_size = 0;
  1499. rel_addr = 0;
  1500. /* leave one program header for the program interpreter */
  1501. ph = &phdr[0];
  1502. if (interp)
  1503. ph++;
  1504. for(j = 0; j < 2; j++) {
  1505. ph->p_type = PT_LOAD;
  1506. if (j == 0)
  1507. ph->p_flags = PF_R | PF_X;
  1508. else
  1509. ph->p_flags = PF_R | PF_W;
  1510. ph->p_align = ELF_PAGE_SIZE;
  1511. /* we do the following ordering: interp, symbol tables,
  1512. relocations, progbits, nobits */
  1513. /* XXX: do faster and simpler sorting */
  1514. for(k = 0; k < 5; k++) {
  1515. for(i = 1; i < s1->nb_sections; i++) {
  1516. s = s1->sections[i];
  1517. /* compute if section should be included */
  1518. if (j == 0) {
  1519. if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
  1520. SHF_ALLOC)
  1521. continue;
  1522. } else {
  1523. if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) !=
  1524. (SHF_ALLOC | SHF_WRITE))
  1525. continue;
  1526. }
  1527. if (s == interp) {
  1528. if (k != 0)
  1529. continue;
  1530. } else if (s->sh_type == SHT_DYNSYM ||
  1531. s->sh_type == SHT_STRTAB ||
  1532. s->sh_type == SHT_HASH) {
  1533. if (k != 1)
  1534. continue;
  1535. } else if (s->sh_type == SHT_RELX) {
  1536. if (k != 2)
  1537. continue;
  1538. } else if (s->sh_type == SHT_NOBITS) {
  1539. if (k != 4)
  1540. continue;
  1541. } else {
  1542. if (k != 3)
  1543. continue;
  1544. }
  1545. section_order[sh_order_index++] = i;
  1546. /* section matches: we align it and add its size */
  1547. tmp = addr;
  1548. addr = (addr + s->sh_addralign - 1) &
  1549. ~(s->sh_addralign - 1);
  1550. file_offset += addr - tmp;
  1551. s->sh_offset = file_offset;
  1552. s->sh_addr = addr;
  1553. /* update program header infos */
  1554. if (ph->p_offset == 0) {
  1555. ph->p_offset = file_offset;
  1556. ph->p_vaddr = addr;
  1557. ph->p_paddr = ph->p_vaddr;
  1558. }
  1559. /* update dynamic relocation infos */
  1560. if (s->sh_type == SHT_RELX) {
  1561. if (rel_size == 0)
  1562. rel_addr = addr;
  1563. rel_size += s->sh_size;
  1564. }
  1565. addr += s->sh_size;
  1566. if (s->sh_type != SHT_NOBITS)
  1567. file_offset += s->sh_size;
  1568. }
  1569. }
  1570. ph->p_filesz = file_offset - ph->p_offset;
  1571. ph->p_memsz = addr - ph->p_vaddr;
  1572. ph++;
  1573. if (j == 0) {
  1574. if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
  1575. /* if in the middle of a page, we duplicate the page in
  1576. memory so that one copy is RX and the other is RW */
  1577. if ((addr & (ELF_PAGE_SIZE - 1)) != 0)
  1578. addr += ELF_PAGE_SIZE;
  1579. } else {
  1580. addr = (addr + ELF_PAGE_SIZE - 1) & ~(ELF_PAGE_SIZE - 1);
  1581. file_offset = (file_offset + ELF_PAGE_SIZE - 1) &
  1582. ~(ELF_PAGE_SIZE - 1);
  1583. }
  1584. }
  1585. }
  1586. /* if interpreter, then add corresponing program header */
  1587. if (interp) {
  1588. ph = &phdr[0];
  1589. ph->p_type = PT_INTERP;
  1590. ph->p_offset = interp->sh_offset;
  1591. ph->p_vaddr = interp->sh_addr;
  1592. ph->p_paddr = ph->p_vaddr;
  1593. ph->p_filesz = interp->sh_size;
  1594. ph->p_memsz = interp->sh_size;
  1595. ph->p_flags = PF_R;
  1596. ph->p_align = interp->sh_addralign;
  1597. }
  1598. /* if dynamic section, then add corresponing program header */
  1599. if (dynamic) {
  1600. ElfW(Sym) *sym_end;
  1601. ph = &phdr[phnum - 1];
  1602. ph->p_type = PT_DYNAMIC;
  1603. ph->p_offset = dynamic->sh_offset;
  1604. ph->p_vaddr = dynamic->sh_addr;
  1605. ph->p_paddr = ph->p_vaddr;
  1606. ph->p_filesz = dynamic->sh_size;
  1607. ph->p_memsz = dynamic->sh_size;
  1608. ph->p_flags = PF_R | PF_W;
  1609. ph->p_align = dynamic->sh_addralign;
  1610. /* put GOT dynamic section address */
  1611. put32(s1->got->data, dynamic->sh_addr);
  1612. /* relocate the PLT */
  1613. if (file_type == TCC_OUTPUT_EXE
  1614. #if defined(TCC_OUTPUT_DLL_WITH_PLT)
  1615. || file_type == TCC_OUTPUT_DLL
  1616. #endif
  1617. ) {
  1618. uint8_t *p, *p_end;
  1619. p = s1->plt->data;
  1620. p_end = p + s1->plt->data_offset;
  1621. if (p < p_end) {
  1622. #if defined(TCC_TARGET_I386)
  1623. put32(p + 2, get32(p + 2) + s1->got->sh_addr);
  1624. put32(p + 8, get32(p + 8) + s1->got->sh_addr);
  1625. p += 16;
  1626. while (p < p_end) {
  1627. put32(p + 2, get32(p + 2) + s1->got->sh_addr);
  1628. p += 16;
  1629. }
  1630. #elif defined(TCC_TARGET_X86_64)
  1631. int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
  1632. put32(p + 2, get32(p + 2) + x);
  1633. put32(p + 8, get32(p + 8) + x - 6);
  1634. p += 16;
  1635. while (p < p_end) {
  1636. put32(p + 2, get32(p + 2) + x + s1->plt->data - p);
  1637. p += 16;
  1638. }
  1639. #elif defined(TCC_TARGET_ARM)
  1640. int x;
  1641. x=s1->got->sh_addr - s1->plt->sh_addr - 12;
  1642. p +=16;
  1643. while (p < p_end) {
  1644. put32(p + 12, x + get32(p + 12) + s1->plt->data - p);
  1645. p += 16;
  1646. }
  1647. #elif defined(TCC_TARGET_C67)
  1648. /* XXX: TODO */
  1649. #else
  1650. #error unsupported CPU
  1651. #endif
  1652. }
  1653. }
  1654. /* relocate symbols in .dynsym */
  1655. sym_end = (ElfW(Sym) *)(s1->dynsym->data + s1->dynsym->data_offset);
  1656. for(sym = (ElfW(Sym) *)s1->dynsym->data + 1;
  1657. sym < sym_end;
  1658. sym++) {
  1659. if (sym->st_shndx == SHN_UNDEF) {
  1660. /* relocate to the PLT if the symbol corresponds
  1661. to a PLT entry */
  1662. if (sym->st_value)
  1663. sym->st_value += s1->plt->sh_addr;
  1664. } else if (sym->st_shndx < SHN_LORESERVE) {
  1665. /* do symbol relocation */
  1666. sym->st_value += s1->sections[sym->st_shndx]->sh_addr;
  1667. }
  1668. }
  1669. /* put dynamic section entries */
  1670. dynamic->data_offset = saved_dynamic_data_offset;
  1671. put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
  1672. put_dt(dynamic, DT_STRTAB, dynstr->sh_addr);
  1673. put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
  1674. put_dt(dynamic, DT_STRSZ, dynstr->data_offset);
  1675. put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));
  1676. #ifdef TCC_TARGET_X86_64
  1677. put_dt(dynamic, DT_RELA, rel_addr);
  1678. put_dt(dynamic, DT_RELASZ, rel_size);
  1679. put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
  1680. #else
  1681. put_dt(dynamic, DT_REL, rel_addr);
  1682. put_dt(dynamic, DT_RELSZ, rel_size);
  1683. put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
  1684. #endif
  1685. if (s1->do_debug)
  1686. put_dt(dynamic, DT_DEBUG, 0);
  1687. put_dt(dynamic, DT_NULL, 0);
  1688. }
  1689. ehdr.e_phentsize = sizeof(ElfW(Phdr));
  1690. ehdr.e_phnum = phnum;
  1691. ehdr.e_phoff = sizeof(ElfW(Ehdr));
  1692. }
  1693. /* all other sections come after */
  1694. for(i = 1; i < s1->nb_sections; i++) {
  1695. s = s1->sections[i];
  1696. if (phnum > 0 && (s->sh_flags & SHF_ALLOC))
  1697. continue;
  1698. section_order[sh_order_index++] = i;
  1699. file_offset = (file_offset + s->sh_addralign - 1) &
  1700. ~(s->sh_addralign - 1);
  1701. s->sh_offset = file_offset;
  1702. if (s->sh_type != SHT_NOBITS)
  1703. file_offset += s->sh_size;
  1704. }
  1705. /* if building executable or DLL, then relocate each section
  1706. except the GOT which is already relocated */
  1707. if (file_type != TCC_OUTPUT_OBJ) {
  1708. relocate_syms(s1, 0);
  1709. if (s1->nb_errors != 0) {
  1710. fail:
  1711. ret = -1;
  1712. goto the_end;
  1713. }
  1714. /* relocate sections */
  1715. /* XXX: ignore sections with allocated relocations ? */
  1716. for(i = 1; i < s1->nb_sections; i++) {
  1717. s = s1->sections[i];
  1718. if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr
  1719. relocate_section(s1, s);
  1720. }
  1721. /* relocate relocation entries if the relocation tables are
  1722. allocated in the executable */
  1723. for(i = 1; i < s1->nb_sections; i++) {
  1724. s = s1->sections[i];
  1725. if ((s->sh_flags & SHF_ALLOC) &&
  1726. s->sh_type == SHT_RELX) {
  1727. relocate_rel(s1, s);
  1728. }
  1729. }
  1730. /* get entry point address */
  1731. if (file_type == TCC_OUTPUT_EXE)
  1732. ehdr.e_entry = (unsigned long)tcc_get_symbol_err(s1, "_start");
  1733. else
  1734. ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
  1735. }
  1736. /* write elf file */
  1737. if (file_type == TCC_OUTPUT_OBJ)
  1738. mode = 0666;
  1739. else
  1740. mode = 0777;
  1741. fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
  1742. if (fd < 0) {
  1743. error_noabort("could not write '%s'", filename);
  1744. goto fail;
  1745. }
  1746. f = fdopen(fd, "wb");
  1747. if (s1->verbose)
  1748. printf("<- %s\n", filename);
  1749. #ifdef TCC_TARGET_COFF
  1750. if (s1->output_format == TCC_OUTPUT_FORMAT_COFF) {
  1751. tcc_output_coff(s1, f);
  1752. } else
  1753. #endif
  1754. if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) {
  1755. sort_syms(s1, symtab_section);
  1756. /* align to 4 */
  1757. file_offset = (file_offset + 3) & -4;
  1758. /* fill header */
  1759. ehdr.e_ident[0] = ELFMAG0;
  1760. ehdr.e_ident[1] = ELFMAG1;
  1761. ehdr.e_ident[2] = ELFMAG2;
  1762. ehdr.e_ident[3] = ELFMAG3;
  1763. ehdr.e_ident[4] = TCC_ELFCLASS;
  1764. ehdr.e_ident[5] = ELFDATA2LSB;
  1765. ehdr.e_ident[6] = EV_CURRENT;
  1766. #ifdef __FreeBSD__
  1767. ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
  1768. #endif
  1769. #ifdef TCC_TARGET_ARM
  1770. #ifdef TCC_ARM_EABI
  1771. ehdr.e_ident[EI_OSABI] = 0;
  1772. ehdr.e_flags = 4 << 24;
  1773. #else
  1774. ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
  1775. #endif
  1776. #endif
  1777. switch(file_type) {
  1778. default:
  1779. case TCC_OUTPUT_EXE:
  1780. ehdr.e_type = ET_EXEC;
  1781. break;
  1782. case TCC_OUTPUT_DLL:
  1783. ehdr.e_type = ET_DYN;
  1784. break;
  1785. case TCC_OUTPUT_OBJ:
  1786. ehdr.e_type = ET_REL;
  1787. break;
  1788. }
  1789. ehdr.e_machine = EM_TCC_TARGET;
  1790. ehdr.e_version = EV_CURRENT;
  1791. ehdr.e_shoff = file_offset;
  1792. ehdr.e_ehsize = sizeof(ElfW(Ehdr));
  1793. ehdr.e_shentsize = sizeof(ElfW(Shdr));
  1794. ehdr.e_shnum = shnum;
  1795. ehdr.e_shstrndx = shnum - 1;
  1796. fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
  1797. fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);
  1798. offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr));
  1799. for(i=1;i<s1->nb_sections;i++) {
  1800. s = s1->sections[section_order[i]];
  1801. if (s->sh_type != SHT_NOBITS) {
  1802. while (offset < s->sh_offset) {
  1803. fputc(0, f);
  1804. offset++;
  1805. }
  1806. size = s->sh_size;
  1807. fwrite(s->data, 1, size, f);
  1808. offset += size;
  1809. }
  1810. }
  1811. /* output section headers */
  1812. while (offset < ehdr.e_shoff) {
  1813. fputc(0, f);
  1814. offset++;
  1815. }
  1816. for(i=0;i<s1->nb_sections;i++) {
  1817. sh = &shdr;
  1818. memset(sh, 0, sizeof(ElfW(Shdr)));
  1819. s = s1->sections[i];
  1820. if (s) {
  1821. sh->sh_name = s->sh_name;
  1822. sh->sh_type = s->sh_type;
  1823. sh->sh_flags = s->sh_flags;
  1824. sh->sh_entsize = s->sh_entsize;
  1825. sh->sh_info = s->sh_info;
  1826. if (s->link)
  1827. sh->sh_link = s->link->sh_num;
  1828. sh->sh_addralign = s->sh_addralign;
  1829. sh->sh_addr = s->sh_addr;
  1830. sh->sh_offset = s->sh_offset;
  1831. sh->sh_size = s->sh_size;
  1832. }
  1833. fwrite(sh, 1, sizeof(ElfW(Shdr)), f);
  1834. }
  1835. } else {
  1836. tcc_output_binary(s1, f, section_order);
  1837. }
  1838. fclose(f);
  1839. ret = 0;
  1840. the_end:
  1841. tcc_free(s1->symtab_to_dynsym);
  1842. tcc_free(section_order);
  1843. tcc_free(phdr);
  1844. tcc_free(s1->got_offsets);
  1845. return ret;
  1846. }
  1847. int tcc_output_file(TCCState *s, const char *filename)
  1848. {
  1849. int ret;
  1850. #ifdef TCC_TARGET_PE
  1851. if (s->output_type != TCC_OUTPUT_OBJ) {
  1852. ret = pe_output_file(s, filename);
  1853. } else
  1854. #endif
  1855. {
  1856. ret = elf_output_file(s, filename);
  1857. }
  1858. return ret;
  1859. }
  1860. static void *load_data(int fd, unsigned long file_offset, unsigned long size)
  1861. {
  1862. void *data;
  1863. data = tcc_malloc(size);
  1864. lseek(fd, file_offset, SEEK_SET);
  1865. read(fd, data, size);
  1866. return data;
  1867. }
  1868. typedef struct SectionMergeInfo {
  1869. Section *s; /* corresponding existing section */
  1870. unsigned long offset; /* offset of the new section in the existing section */
  1871. uint8_t new_section; /* true if section 's' was added */
  1872. uint8_t link_once; /* true if link once section */
  1873. } SectionMergeInfo;
  1874. /* load an object file and merge it with current files */
  1875. /* XXX: handle correctly stab (debug) info */
  1876. static int tcc_load_object_file(TCCState *s1,
  1877. int fd, unsigned long file_offset)
  1878. {
  1879. ElfW(Ehdr) ehdr;
  1880. ElfW(Shdr) *shdr, *sh;
  1881. int size, i, j, offset, offseti, nb_syms, sym_index, ret;
  1882. unsigned char *strsec, *strtab;
  1883. int *old_to_new_syms;
  1884. char *sh_name, *name;
  1885. SectionMergeInfo *sm_table, *sm;
  1886. ElfW(Sym) *sym, *symtab;
  1887. ElfW_Rel *rel, *rel_end;
  1888. Section *s;
  1889. int stab_index;
  1890. int stabstr_index;
  1891. stab_index = stabstr_index = 0;
  1892. if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
  1893. goto fail1;
  1894. if (ehdr.e_ident[0] != ELFMAG0 ||
  1895. ehdr.e_ident[1] != ELFMAG1 ||
  1896. ehdr.e_ident[2] != ELFMAG2 ||
  1897. ehdr.e_ident[3] != ELFMAG3)
  1898. goto fail1;
  1899. /* test if object file */
  1900. if (ehdr.e_type != ET_REL)
  1901. goto fail1;
  1902. /* test CPU specific stuff */
  1903. if (ehdr.e_ident[5] != ELFDATA2LSB ||
  1904. ehdr.e_machine != EM_TCC_TARGET) {
  1905. fail1:
  1906. error_noabort("invalid object file");
  1907. return -1;
  1908. }
  1909. /* read sections */
  1910. shdr = load_data(fd, file_offset + ehdr.e_shoff,
  1911. sizeof(ElfW(Shdr)) * ehdr.e_shnum);
  1912. sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum);
  1913. /* load section names */
  1914. sh = &shdr[ehdr.e_shstrndx];
  1915. strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
  1916. /* load symtab and strtab */
  1917. old_to_new_syms = NULL;
  1918. symtab = NULL;
  1919. strtab = NULL;
  1920. nb_syms = 0;
  1921. for(i = 1; i < ehdr.e_shnum; i++) {
  1922. sh = &shdr[i];
  1923. if (sh->sh_type == SHT_SYMTAB) {
  1924. if (symtab) {
  1925. error_noabort("object must contain only one symtab");
  1926. fail:
  1927. ret = -1;
  1928. goto the_end;
  1929. }
  1930. nb_syms = sh->sh_size / sizeof(ElfW(Sym));
  1931. symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
  1932. sm_table[i].s = symtab_section;
  1933. /* now load strtab */
  1934. sh = &shdr[sh->sh_link];
  1935. strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size);
  1936. }
  1937. }
  1938. /* now examine each section and try to merge its content with the
  1939. ones in memory */
  1940. for(i = 1; i < ehdr.e_shnum; i++) {
  1941. /* no need to examine section name strtab */
  1942. if (i == ehdr.e_shstrndx)
  1943. continue;
  1944. sh = &shdr[i];
  1945. sh_name = strsec + sh->sh_name;
  1946. /* ignore sections types we do not handle */
  1947. if (sh->sh_type != SHT_PROGBITS &&
  1948. sh->sh_type != SHT_RELX &&
  1949. #ifdef TCC_ARM_EABI
  1950. sh->sh_type != SHT_ARM_EXIDX &&
  1951. #endif
  1952. sh->sh_type != SHT_NOBITS &&
  1953. strcmp(sh_name, ".stabstr")
  1954. )
  1955. continue;
  1956. if (sh->sh_addralign < 1)
  1957. sh->sh_addralign = 1;
  1958. /* find corresponding section, if any */
  1959. for(j = 1; j < s1->nb_sections;j++) {
  1960. s = s1->sections[j];
  1961. if (!strcmp(s->name, sh_name)) {
  1962. if (!strncmp(sh_name, ".gnu.linkonce",
  1963. sizeof(".gnu.linkonce") - 1)) {
  1964. /* if a 'linkonce' section is already present, we
  1965. do not add it again. It is a little tricky as
  1966. symbols can still be defined in
  1967. it. */
  1968. sm_table[i].link_once = 1;
  1969. goto next;
  1970. } else {
  1971. goto found;
  1972. }
  1973. }
  1974. }
  1975. /* not found: create new section */
  1976. s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags);
  1977. /* take as much info as possible from the section. sh_link and
  1978. sh_info will be updated later */
  1979. s->sh_addralign = sh->sh_addralign;
  1980. s->sh_entsize = sh->sh_entsize;
  1981. sm_table[i].new_section = 1;
  1982. found:
  1983. if (sh->sh_type != s->sh_type) {
  1984. error_noabort("invalid section type");
  1985. goto fail;
  1986. }
  1987. /* align start of section */
  1988. offset = s->data_offset;
  1989. if (0 == strcmp(sh_name, ".stab")) {
  1990. stab_index = i;
  1991. goto no_align;
  1992. }
  1993. if (0 == strcmp(sh_name, ".stabstr")) {
  1994. stabstr_index = i;
  1995. goto no_align;
  1996. }
  1997. size = sh->sh_addralign - 1;
  1998. offset = (offset + size) & ~size;
  1999. if (sh->sh_addralign > s->sh_addralign)
  2000. s->sh_addralign = sh->sh_addralign;
  2001. s->data_offset = offset;
  2002. no_align:
  2003. sm_table[i].offset = offset;
  2004. sm_table[i].s = s;
  2005. /* concatenate sections */
  2006. size = sh->sh_size;
  2007. if (sh->sh_type != SHT_NOBITS) {
  2008. unsigned char *ptr;
  2009. lseek(fd, file_offset + sh->sh_offset, SEEK_SET);
  2010. ptr = section_ptr_add(s, size);
  2011. read(fd, ptr, size);
  2012. } else {
  2013. s->data_offset += size;
  2014. }
  2015. next: ;
  2016. }
  2017. /* //gr relocate stab strings */
  2018. if (stab_index && stabstr_index) {
  2019. Stab_Sym *a, *b;
  2020. unsigned o;
  2021. s = sm_table[stab_index].s;
  2022. a = (Stab_Sym *)(s->data + sm_table[stab_index].offset);
  2023. b = (Stab_Sym *)(s->data + s->data_offset);
  2024. o = sm_table[stabstr_index].offset;
  2025. while (a < b)
  2026. a->n_strx += o, a++;
  2027. }
  2028. /* second short pass to update sh_link and sh_info fields of new
  2029. sections */
  2030. for(i = 1; i < ehdr.e_shnum; i++) {
  2031. s = sm_table[i].s;
  2032. if (!s || !sm_table[i].new_section)
  2033. continue;
  2034. sh = &shdr[i];
  2035. if (sh->sh_link > 0)
  2036. s->link = sm_table[sh->sh_link].s;
  2037. if (sh->sh_type == SHT_RELX) {
  2038. s->sh_info = sm_table[sh->sh_info].s->sh_num;
  2039. /* update backward link */
  2040. s1->sections[s->sh_info]->reloc = s;
  2041. }
  2042. }
  2043. sm = sm_table;
  2044. /* resolve symbols */
  2045. old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int));
  2046. sym = symtab + 1;
  2047. for(i = 1; i < nb_syms; i++, sym++) {
  2048. if (sym->st_shndx != SHN_UNDEF &&
  2049. sym->st_shndx < SHN_LORESERVE) {
  2050. sm = &sm_table[sym->st_shndx];
  2051. if (sm->link_once) {
  2052. /* if a symbol is in a link once section, we use the
  2053. already defined symbol. It is very important to get
  2054. correct relocations */
  2055. if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
  2056. name = strtab + sym->st_name;
  2057. sym_index = find_elf_sym(symtab_section, name);
  2058. if (sym_index)
  2059. old_to_new_syms[i] = sym_index;
  2060. }
  2061. continue;
  2062. }
  2063. /* if no corresponding section added, no need to add symbol */
  2064. if (!sm->s)
  2065. continue;
  2066. /* convert section number */
  2067. sym->st_shndx = sm->s->sh_num;
  2068. /* offset value */
  2069. sym->st_value += sm->offset;
  2070. }
  2071. /* add symbol */
  2072. name = strtab + sym->st_name;
  2073. sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size,
  2074. sym->st_info, sym->st_other,
  2075. sym->st_shndx, name);
  2076. old_to_new_syms[i] = sym_index;
  2077. }
  2078. /* third pass to patch relocation entries */
  2079. for(i = 1; i < ehdr.e_shnum; i++) {
  2080. s = sm_table[i].s;
  2081. if (!s)
  2082. continue;
  2083. sh = &shdr[i];
  2084. offset = sm_table[i].offset;
  2085. switch(s->sh_type) {
  2086. case SHT_RELX:
  2087. /* take relocation offset information */
  2088. offseti = sm_table[sh->sh_info].offset;
  2089. rel_end = (ElfW_Rel *)(s->data + s->data_offset);
  2090. for(rel = (ElfW_Rel *)(s->data + offset);
  2091. rel < rel_end;
  2092. rel++) {
  2093. int type;
  2094. unsigned sym_index;
  2095. /* convert symbol index */
  2096. type = ELFW(R_TYPE)(rel->r_info);
  2097. sym_index = ELFW(R_SYM)(rel->r_info);
  2098. /* NOTE: only one symtab assumed */
  2099. if (sym_index >= nb_syms)
  2100. goto invalid_reloc;
  2101. sym_index = old_to_new_syms[sym_index];
  2102. /* ignore link_once in rel section. */
  2103. if (!sym_index && !sm->link_once) {
  2104. invalid_reloc:
  2105. error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x",
  2106. i, strsec + sh->sh_name, rel->r_offset);
  2107. goto fail;
  2108. }
  2109. rel->r_info = ELFW(R_INFO)(sym_index, type);
  2110. /* offset the relocation offset */
  2111. rel->r_offset += offseti;
  2112. }
  2113. break;
  2114. default:
  2115. break;
  2116. }
  2117. }
  2118. ret = 0;
  2119. the_end:
  2120. tcc_free(symtab);
  2121. tcc_free(strtab);
  2122. tcc_free(old_to_new_syms);
  2123. tcc_free(sm_table);
  2124. tcc_free(strsec);
  2125. tcc_free(shdr);
  2126. return ret;
  2127. }
  2128. #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
  2129. typedef struct ArchiveHeader {
  2130. char ar_name[16]; /* name of this member */
  2131. char ar_date[12]; /* file mtime */
  2132. char ar_uid[6]; /* owner uid; printed as decimal */
  2133. char ar_gid[6]; /* owner gid; printed as decimal */
  2134. char ar_mode[8]; /* file mode, printed as octal */
  2135. char ar_size[10]; /* file size, printed as decimal */
  2136. char ar_fmag[2]; /* should contain ARFMAG */
  2137. } ArchiveHeader;
  2138. static int get_be32(const uint8_t *b)
  2139. {
  2140. return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
  2141. }
  2142. /* load only the objects which resolve undefined symbols */
  2143. static int tcc_load_alacarte(TCCState *s1, int fd, int size)
  2144. {
  2145. int i, bound, nsyms, sym_index, off, ret;
  2146. uint8_t *data;
  2147. const char *ar_names, *p;
  2148. const uint8_t *ar_index;
  2149. ElfW(Sym) *sym;
  2150. data = tcc_malloc(size);
  2151. if (read(fd, data, size) != size)
  2152. goto fail;
  2153. nsyms = get_be32(data);
  2154. ar_index = data + 4;
  2155. ar_names = ar_index + nsyms * 4;
  2156. do {
  2157. bound = 0;
  2158. for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) {
  2159. sym_index = find_elf_sym(symtab_section, p);
  2160. if(sym_index) {
  2161. sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
  2162. if(sym->st_shndx == SHN_UNDEF) {
  2163. off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader);
  2164. #if 0
  2165. printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx);
  2166. #endif
  2167. ++bound;
  2168. lseek(fd, off, SEEK_SET);
  2169. if(tcc_load_object_file(s1, fd, off) < 0) {
  2170. fail:
  2171. ret = -1;
  2172. goto the_end;
  2173. }
  2174. }
  2175. }
  2176. }
  2177. } while(bound);
  2178. ret = 0;
  2179. the_end:
  2180. tcc_free(data);
  2181. return ret;
  2182. }
  2183. /* load a '.a' file */
  2184. static int tcc_load_archive(TCCState *s1, int fd)
  2185. {
  2186. ArchiveHeader hdr;
  2187. char ar_size[11];
  2188. char ar_name[17];
  2189. char magic[8];
  2190. int size, len, i;
  2191. unsigned long file_offset;
  2192. /* skip magic which was already checked */
  2193. read(fd, magic, sizeof(magic));
  2194. for(;;) {
  2195. len = read(fd, &hdr, sizeof(hdr));
  2196. if (len == 0)
  2197. break;
  2198. if (len != sizeof(hdr)) {
  2199. error_noabort("invalid archive");
  2200. return -1;
  2201. }
  2202. memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size));
  2203. ar_size[sizeof(hdr.ar_size)] = '\0';
  2204. size = strtol(ar_size, NULL, 0);
  2205. memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name));
  2206. for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) {
  2207. if (ar_name[i] != ' ')
  2208. break;
  2209. }
  2210. ar_name[i + 1] = '\0';
  2211. // printf("name='%s' size=%d %s\n", ar_name, size, ar_size);
  2212. file_offset = lseek(fd, 0, SEEK_CUR);
  2213. /* align to even */
  2214. size = (size + 1) & ~1;
  2215. if (!strcmp(ar_name, "/")) {
  2216. /* coff symbol table : we handle it */
  2217. if(s1->alacarte_link)
  2218. return tcc_load_alacarte(s1, fd, size);
  2219. } else if (!strcmp(ar_name, "//") ||
  2220. !strcmp(ar_name, "__.SYMDEF") ||
  2221. !strcmp(ar_name, "__.SYMDEF/") ||
  2222. !strcmp(ar_name, "ARFILENAMES/")) {
  2223. /* skip symbol table or archive names */
  2224. } else {
  2225. if (tcc_load_object_file(s1, fd, file_offset) < 0)
  2226. return -1;
  2227. }
  2228. lseek(fd, file_offset + size, SEEK_SET);
  2229. }
  2230. return 0;
  2231. }
  2232. /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
  2233. is referenced by the user (so it should be added as DT_NEEDED in
  2234. the generated ELF file) */
  2235. static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
  2236. {
  2237. ElfW(Ehdr) ehdr;
  2238. ElfW(Shdr) *shdr, *sh, *sh1;
  2239. int i, j, nb_syms, nb_dts, sym_bind, ret;
  2240. ElfW(Sym) *sym, *dynsym;
  2241. ElfW(Dyn) *dt, *dynamic;
  2242. unsigned char *dynstr;
  2243. const char *name, *soname;
  2244. DLLReference *dllref;
  2245. read(fd, &ehdr, sizeof(ehdr));
  2246. /* test CPU specific stuff */
  2247. if (ehdr.e_ident[5] != ELFDATA2LSB ||
  2248. ehdr.e_machine != EM_TCC_TARGET) {
  2249. error_noabort("bad architecture");
  2250. return -1;
  2251. }
  2252. /* read sections */
  2253. shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum);
  2254. /* load dynamic section and dynamic symbols */
  2255. nb_syms = 0;
  2256. nb_dts = 0;
  2257. dynamic = NULL;
  2258. dynsym = NULL; /* avoid warning */
  2259. dynstr = NULL; /* avoid warning */
  2260. for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
  2261. switch(sh->sh_type) {
  2262. case SHT_DYNAMIC:
  2263. nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
  2264. dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
  2265. break;
  2266. case SHT_DYNSYM:
  2267. nb_syms = sh->sh_size / sizeof(ElfW(Sym));
  2268. dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
  2269. sh1 = &shdr[sh->sh_link];
  2270. dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size);
  2271. break;
  2272. default:
  2273. break;
  2274. }
  2275. }
  2276. /* compute the real library name */
  2277. soname = tcc_basename(filename);
  2278. for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
  2279. if (dt->d_tag == DT_SONAME) {
  2280. soname = dynstr + dt->d_un.d_val;
  2281. }
  2282. }
  2283. /* if the dll is already loaded, do not load it */
  2284. for(i = 0; i < s1->nb_loaded_dlls; i++) {
  2285. dllref = s1->loaded_dlls[i];
  2286. if (!strcmp(soname, dllref->name)) {
  2287. /* but update level if needed */
  2288. if (level < dllref->level)
  2289. dllref->level = level;
  2290. ret = 0;
  2291. goto the_end;
  2292. }
  2293. }
  2294. // printf("loading dll '%s'\n", soname);
  2295. /* add the dll and its level */
  2296. dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
  2297. dllref->level = level;
  2298. strcpy(dllref->name, soname);
  2299. dynarray_add((void ***)&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
  2300. /* add dynamic symbols in dynsym_section */
  2301. for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
  2302. sym_bind = ELFW(ST_BIND)(sym->st_info);
  2303. if (sym_bind == STB_LOCAL)
  2304. continue;
  2305. name = dynstr + sym->st_name;
  2306. add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
  2307. sym->st_info, sym->st_other, sym->st_shndx, name);
  2308. }
  2309. /* load all referenced DLLs */
  2310. for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) {
  2311. switch(dt->d_tag) {
  2312. case DT_NEEDED:
  2313. name = dynstr + dt->d_un.d_val;
  2314. for(j = 0; j < s1->nb_loaded_dlls; j++) {
  2315. dllref = s1->loaded_dlls[j];
  2316. if (!strcmp(name, dllref->name))
  2317. goto already_loaded;
  2318. }
  2319. if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) {
  2320. error_noabort("referenced dll '%s' not found", name);
  2321. ret = -1;
  2322. goto the_end;
  2323. }
  2324. already_loaded:
  2325. break;
  2326. }
  2327. }
  2328. ret = 0;
  2329. the_end:
  2330. tcc_free(dynstr);
  2331. tcc_free(dynsym);
  2332. tcc_free(dynamic);
  2333. tcc_free(shdr);
  2334. return ret;
  2335. }
  2336. #define LD_TOK_NAME 256
  2337. #define LD_TOK_EOF (-1)
  2338. /* return next ld script token */
  2339. static int ld_next(TCCState *s1, char *name, int name_size)
  2340. {
  2341. int c;
  2342. char *q;
  2343. redo:
  2344. switch(ch) {
  2345. case ' ':
  2346. case '\t':
  2347. case '\f':
  2348. case '\v':
  2349. case '\r':
  2350. case '\n':
  2351. inp();
  2352. goto redo;
  2353. case '/':
  2354. minp();
  2355. if (ch == '*') {
  2356. file->buf_ptr = parse_comment(file->buf_ptr);
  2357. ch = file->buf_ptr[0];
  2358. goto redo;
  2359. } else {
  2360. q = name;
  2361. *q++ = '/';
  2362. goto parse_name;
  2363. }
  2364. break;
  2365. /* case 'a' ... 'z': */
  2366. case 'a':
  2367. case 'b':
  2368. case 'c':
  2369. case 'd':
  2370. case 'e':
  2371. case 'f':
  2372. case 'g':
  2373. case 'h':
  2374. case 'i':
  2375. case 'j':
  2376. case 'k':
  2377. case 'l':
  2378. case 'm':
  2379. case 'n':
  2380. case 'o':
  2381. case 'p':
  2382. case 'q':
  2383. case 'r':
  2384. case 's':
  2385. case 't':
  2386. case 'u':
  2387. case 'v':
  2388. case 'w':
  2389. case 'x':
  2390. case 'y':
  2391. case 'z':
  2392. /* case 'A' ... 'z': */
  2393. case 'A':
  2394. case 'B':
  2395. case 'C':
  2396. case 'D':
  2397. case 'E':
  2398. case 'F':
  2399. case 'G':
  2400. case 'H':
  2401. case 'I':
  2402. case 'J':
  2403. case 'K':
  2404. case 'L':
  2405. case 'M':
  2406. case 'N':
  2407. case 'O':
  2408. case 'P':
  2409. case 'Q':
  2410. case 'R':
  2411. case 'S':
  2412. case 'T':
  2413. case 'U':
  2414. case 'V':
  2415. case 'W':
  2416. case 'X':
  2417. case 'Y':
  2418. case 'Z':
  2419. case '_':
  2420. case '\\':
  2421. case '.':
  2422. case '$':
  2423. case '~':
  2424. q = name;
  2425. parse_name:
  2426. for(;;) {
  2427. if (!((ch >= 'a' && ch <= 'z') ||
  2428. (ch >= 'A' && ch <= 'Z') ||
  2429. (ch >= '0' && ch <= '9') ||
  2430. strchr("/.-_+=$:\\,~", ch)))
  2431. break;
  2432. if ((q - name) < name_size - 1) {
  2433. *q++ = ch;
  2434. }
  2435. minp();
  2436. }
  2437. *q = '\0';
  2438. c = LD_TOK_NAME;
  2439. break;
  2440. case CH_EOF:
  2441. c = LD_TOK_EOF;
  2442. break;
  2443. default:
  2444. c = ch;
  2445. inp();
  2446. break;
  2447. }
  2448. #if 0
  2449. printf("tok=%c %d\n", c, c);
  2450. if (c == LD_TOK_NAME)
  2451. printf(" name=%s\n", name);
  2452. #endif
  2453. return c;
  2454. }
  2455. static int ld_add_file_list(TCCState *s1, int as_needed)
  2456. {
  2457. char filename[1024];
  2458. int t, ret;
  2459. t = ld_next(s1, filename, sizeof(filename));
  2460. if (t != '(')
  2461. expect("(");
  2462. t = ld_next(s1, filename, sizeof(filename));
  2463. for(;;) {
  2464. if (t == LD_TOK_EOF) {
  2465. error_noabort("unexpected end of file");
  2466. return -1;
  2467. } else if (t == ')') {
  2468. break;
  2469. } else if (t != LD_TOK_NAME) {
  2470. error_noabort("filename expected");
  2471. return -1;
  2472. }
  2473. if (!strcmp(filename, "AS_NEEDED")) {
  2474. ret = ld_add_file_list(s1, 1);
  2475. if (ret)
  2476. return ret;
  2477. } else {
  2478. /* TODO: Implement AS_NEEDED support. Ignore it for now */
  2479. if (!as_needed)
  2480. tcc_add_file(s1, filename);
  2481. }
  2482. t = ld_next(s1, filename, sizeof(filename));
  2483. if (t == ',') {
  2484. t = ld_next(s1, filename, sizeof(filename));
  2485. }
  2486. }
  2487. return 0;
  2488. }
  2489. /* interpret a subset of GNU ldscripts to handle the dummy libc.so
  2490. files */
  2491. static int tcc_load_ldscript(TCCState *s1)
  2492. {
  2493. char cmd[64];
  2494. char filename[1024];
  2495. int t, ret;
  2496. ch = file->buf_ptr[0];
  2497. ch = handle_eob();
  2498. for(;;) {
  2499. t = ld_next(s1, cmd, sizeof(cmd));
  2500. if (t == LD_TOK_EOF)
  2501. return 0;
  2502. else if (t != LD_TOK_NAME)
  2503. return -1;
  2504. if (!strcmp(cmd, "INPUT") ||
  2505. !strcmp(cmd, "GROUP")) {
  2506. ret = ld_add_file_list(s1, 0);
  2507. if (ret)
  2508. return ret;
  2509. } else if (!strcmp(cmd, "OUTPUT_FORMAT") ||
  2510. !strcmp(cmd, "TARGET")) {
  2511. /* ignore some commands */
  2512. t = ld_next(s1, cmd, sizeof(cmd));
  2513. if (t != '(')
  2514. expect("(");
  2515. for(;;) {
  2516. t = ld_next(s1, filename, sizeof(filename));
  2517. if (t == LD_TOK_EOF) {
  2518. error_noabort("unexpected end of file");
  2519. return -1;
  2520. } else if (t == ')') {
  2521. break;
  2522. }
  2523. }
  2524. } else {
  2525. return -1;
  2526. }
  2527. }
  2528. return 0;
  2529. }