plugin.cc 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  1. // plugin.cc -- plugin manager for gold -*- C++ -*-
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. // Written by Cary Coutant <ccoutant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cerrno>
  19. #include <cstdio>
  20. #include <cstdarg>
  21. #include <cstring>
  22. #include <string>
  23. #include <vector>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include "libiberty.h"
  27. #ifdef ENABLE_PLUGINS
  28. #ifdef HAVE_DLFCN_H
  29. #include <dlfcn.h>
  30. #elif defined (HAVE_WINDOWS_H)
  31. #include <windows.h>
  32. #else
  33. #error Unknown how to handle dynamic-load-libraries.
  34. #endif
  35. #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
  36. #define RTLD_NOW 0 /* Dummy value. */
  37. static void *
  38. dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
  39. {
  40. return LoadLibrary(file);
  41. }
  42. static void *
  43. dlsym(void *handle, const char *name)
  44. {
  45. return reinterpret_cast<void *>(
  46. GetProcAddress(static_cast<HMODULE>(handle),name));
  47. }
  48. static const char *
  49. dlerror(void)
  50. {
  51. return "unable to load dll";
  52. }
  53. #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
  54. #endif /* ENABLE_PLUGINS */
  55. #include "parameters.h"
  56. #include "debug.h"
  57. #include "errors.h"
  58. #include "fileread.h"
  59. #include "layout.h"
  60. #include "options.h"
  61. #include "plugin.h"
  62. #include "target.h"
  63. #include "readsyms.h"
  64. #include "symtab.h"
  65. #include "descriptors.h"
  66. #include "elfcpp.h"
  67. namespace gold
  68. {
  69. #ifdef ENABLE_PLUGINS
  70. // The linker's exported interfaces.
  71. extern "C"
  72. {
  73. static enum ld_plugin_status
  74. register_claim_file(ld_plugin_claim_file_handler handler);
  75. static enum ld_plugin_status
  76. register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
  77. static enum ld_plugin_status
  78. register_cleanup(ld_plugin_cleanup_handler handler);
  79. static enum ld_plugin_status
  80. add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
  81. static enum ld_plugin_status
  82. get_input_file(const void *handle, struct ld_plugin_input_file *file);
  83. static enum ld_plugin_status
  84. get_view(const void *handle, const void **viewp);
  85. static enum ld_plugin_status
  86. release_input_file(const void *handle);
  87. static enum ld_plugin_status
  88. get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
  89. static enum ld_plugin_status
  90. get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
  91. static enum ld_plugin_status
  92. get_symbols_v3(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
  93. static enum ld_plugin_status
  94. add_input_file(const char *pathname);
  95. static enum ld_plugin_status
  96. add_input_library(const char *pathname);
  97. static enum ld_plugin_status
  98. set_extra_library_path(const char *path);
  99. static enum ld_plugin_status
  100. message(int level, const char *format, ...);
  101. static enum ld_plugin_status
  102. get_input_section_count(const void* handle, unsigned int* count);
  103. static enum ld_plugin_status
  104. get_input_section_type(const struct ld_plugin_section section,
  105. unsigned int* type);
  106. static enum ld_plugin_status
  107. get_input_section_name(const struct ld_plugin_section section,
  108. char** section_name_ptr);
  109. static enum ld_plugin_status
  110. get_input_section_contents(const struct ld_plugin_section section,
  111. const unsigned char** section_contents,
  112. size_t* len);
  113. static enum ld_plugin_status
  114. update_section_order(const struct ld_plugin_section *section_list,
  115. unsigned int num_sections);
  116. static enum ld_plugin_status
  117. allow_section_ordering();
  118. static enum ld_plugin_status
  119. allow_unique_segment_for_sections();
  120. static enum ld_plugin_status
  121. unique_segment_for_sections(const char* segment_name,
  122. uint64_t flags,
  123. uint64_t align,
  124. const struct ld_plugin_section *section_list,
  125. unsigned int num_sections);
  126. static enum ld_plugin_status
  127. get_input_section_alignment(const struct ld_plugin_section section,
  128. unsigned int* addralign);
  129. static enum ld_plugin_status
  130. get_input_section_size(const struct ld_plugin_section section,
  131. uint64_t* secsize);
  132. static enum ld_plugin_status
  133. register_new_input(ld_plugin_new_input_handler handler);
  134. static enum ld_plugin_status
  135. get_wrap_symbols(uint64_t *num_symbols, const char ***wrap_symbol_list);
  136. };
  137. #endif // ENABLE_PLUGINS
  138. static Pluginobj* make_sized_plugin_object(const std::string& filename,
  139. Input_file* input_file,
  140. off_t offset, off_t filesize);
  141. // Plugin methods.
  142. // Load one plugin library.
  143. void
  144. Plugin::load()
  145. {
  146. #ifdef ENABLE_PLUGINS
  147. // Load the plugin library.
  148. // FIXME: Look for the library in standard locations.
  149. this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
  150. if (this->handle_ == NULL)
  151. {
  152. gold_error(_("%s: could not load plugin library: %s"),
  153. this->filename_.c_str(), dlerror());
  154. return;
  155. }
  156. // Find the plugin's onload entry point.
  157. void* ptr = dlsym(this->handle_, "onload");
  158. if (ptr == NULL)
  159. {
  160. gold_error(_("%s: could not find onload entry point"),
  161. this->filename_.c_str());
  162. return;
  163. }
  164. ld_plugin_onload onload;
  165. gold_assert(sizeof(onload) == sizeof(ptr));
  166. memcpy(&onload, &ptr, sizeof(ptr));
  167. // Get the linker's version number.
  168. const char* ver = get_version_string();
  169. int major = 0;
  170. int minor = 0;
  171. sscanf(ver, "%d.%d", &major, &minor);
  172. // Allocate and populate a transfer vector.
  173. const int tv_fixed_size = 31;
  174. int tv_size = this->args_.size() + tv_fixed_size;
  175. ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
  176. // Put LDPT_MESSAGE at the front of the list so the plugin can use it
  177. // while processing subsequent entries.
  178. int i = 0;
  179. tv[i].tv_tag = LDPT_MESSAGE;
  180. tv[i].tv_u.tv_message = message;
  181. ++i;
  182. tv[i].tv_tag = LDPT_API_VERSION;
  183. tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
  184. ++i;
  185. tv[i].tv_tag = LDPT_GOLD_VERSION;
  186. tv[i].tv_u.tv_val = major * 100 + minor;
  187. ++i;
  188. tv[i].tv_tag = LDPT_LINKER_OUTPUT;
  189. if (parameters->options().relocatable())
  190. tv[i].tv_u.tv_val = LDPO_REL;
  191. else if (parameters->options().shared())
  192. tv[i].tv_u.tv_val = LDPO_DYN;
  193. else if (parameters->options().pie())
  194. tv[i].tv_u.tv_val = LDPO_PIE;
  195. else
  196. tv[i].tv_u.tv_val = LDPO_EXEC;
  197. ++i;
  198. tv[i].tv_tag = LDPT_OUTPUT_NAME;
  199. tv[i].tv_u.tv_string = parameters->options().output();
  200. for (unsigned int j = 0; j < this->args_.size(); ++j)
  201. {
  202. ++i;
  203. tv[i].tv_tag = LDPT_OPTION;
  204. tv[i].tv_u.tv_string = this->args_[j].c_str();
  205. }
  206. ++i;
  207. tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
  208. tv[i].tv_u.tv_register_claim_file = register_claim_file;
  209. ++i;
  210. tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
  211. tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
  212. ++i;
  213. tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
  214. tv[i].tv_u.tv_register_cleanup = register_cleanup;
  215. ++i;
  216. tv[i].tv_tag = LDPT_ADD_SYMBOLS;
  217. tv[i].tv_u.tv_add_symbols = add_symbols;
  218. ++i;
  219. tv[i].tv_tag = LDPT_GET_INPUT_FILE;
  220. tv[i].tv_u.tv_get_input_file = get_input_file;
  221. ++i;
  222. tv[i].tv_tag = LDPT_GET_VIEW;
  223. tv[i].tv_u.tv_get_view = get_view;
  224. ++i;
  225. tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
  226. tv[i].tv_u.tv_release_input_file = release_input_file;
  227. ++i;
  228. tv[i].tv_tag = LDPT_GET_SYMBOLS;
  229. tv[i].tv_u.tv_get_symbols = get_symbols;
  230. ++i;
  231. tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
  232. tv[i].tv_u.tv_get_symbols = get_symbols_v2;
  233. ++i;
  234. tv[i].tv_tag = LDPT_GET_SYMBOLS_V3;
  235. tv[i].tv_u.tv_get_symbols = get_symbols_v3;
  236. ++i;
  237. tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
  238. tv[i].tv_u.tv_add_input_file = add_input_file;
  239. ++i;
  240. tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
  241. tv[i].tv_u.tv_add_input_library = add_input_library;
  242. ++i;
  243. tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
  244. tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
  245. ++i;
  246. tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
  247. tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
  248. ++i;
  249. tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
  250. tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
  251. ++i;
  252. tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
  253. tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
  254. ++i;
  255. tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
  256. tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
  257. ++i;
  258. tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
  259. tv[i].tv_u.tv_update_section_order = update_section_order;
  260. ++i;
  261. tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
  262. tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
  263. ++i;
  264. tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
  265. tv[i].tv_u.tv_allow_unique_segment_for_sections
  266. = allow_unique_segment_for_sections;
  267. ++i;
  268. tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
  269. tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
  270. ++i;
  271. tv[i].tv_tag = LDPT_GET_INPUT_SECTION_ALIGNMENT;
  272. tv[i].tv_u.tv_get_input_section_alignment = get_input_section_alignment;
  273. ++i;
  274. tv[i].tv_tag = LDPT_GET_INPUT_SECTION_SIZE;
  275. tv[i].tv_u.tv_get_input_section_size = get_input_section_size;
  276. ++i;
  277. tv[i].tv_tag = LDPT_REGISTER_NEW_INPUT_HOOK;
  278. tv[i].tv_u.tv_register_new_input = register_new_input;
  279. ++i;
  280. tv[i].tv_tag = LDPT_GET_WRAP_SYMBOLS;
  281. tv[i].tv_u.tv_get_wrap_symbols = get_wrap_symbols;
  282. ++i;
  283. tv[i].tv_tag = LDPT_NULL;
  284. tv[i].tv_u.tv_val = 0;
  285. gold_assert(i == tv_size - 1);
  286. // Call the onload entry point.
  287. (*onload)(tv);
  288. delete[] tv;
  289. #endif // ENABLE_PLUGINS
  290. }
  291. // Call the plugin claim-file handler.
  292. inline bool
  293. Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
  294. {
  295. int claimed = 0;
  296. if (this->claim_file_handler_ != NULL)
  297. {
  298. (*this->claim_file_handler_)(plugin_input_file, &claimed);
  299. if (claimed)
  300. return true;
  301. }
  302. return false;
  303. }
  304. // Call the all-symbols-read handler.
  305. inline void
  306. Plugin::all_symbols_read()
  307. {
  308. if (this->all_symbols_read_handler_ != NULL)
  309. (*this->all_symbols_read_handler_)();
  310. }
  311. // Call the new_input handler.
  312. inline void
  313. Plugin::new_input(struct ld_plugin_input_file* plugin_input_file)
  314. {
  315. if (this->new_input_handler_ != NULL)
  316. (*this->new_input_handler_)(plugin_input_file);
  317. }
  318. // Call the cleanup handler.
  319. inline void
  320. Plugin::cleanup()
  321. {
  322. if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
  323. {
  324. // Set this flag before calling to prevent a recursive plunge
  325. // in the event that a plugin's cleanup handler issues a
  326. // fatal error.
  327. this->cleanup_done_ = true;
  328. (*this->cleanup_handler_)();
  329. }
  330. }
  331. // This task is used to rescan archives as needed.
  332. class Plugin_rescan : public Task
  333. {
  334. public:
  335. Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
  336. : this_blocker_(this_blocker), next_blocker_(next_blocker)
  337. { }
  338. ~Plugin_rescan()
  339. {
  340. delete this->this_blocker_;
  341. }
  342. Task_token*
  343. is_runnable()
  344. {
  345. if (this->this_blocker_->is_blocked())
  346. return this->this_blocker_;
  347. return NULL;
  348. }
  349. void
  350. locks(Task_locker* tl)
  351. { tl->add(this, this->next_blocker_); }
  352. void
  353. run(Workqueue*)
  354. { parameters->options().plugins()->rescan(this); }
  355. std::string
  356. get_name() const
  357. { return "Plugin_rescan"; }
  358. private:
  359. Task_token* this_blocker_;
  360. Task_token* next_blocker_;
  361. };
  362. // Plugin_recorder logs plugin actions and saves intermediate files
  363. // for later replay.
  364. class Plugin_recorder
  365. {
  366. public:
  367. Plugin_recorder() : file_count_(0), tempdir_(NULL), logfile_(NULL)
  368. { }
  369. bool
  370. init();
  371. void
  372. claimed_file(const std::string& obj_name, off_t offset, off_t filesize,
  373. const std::string& plugin_name);
  374. void
  375. unclaimed_file(const std::string& obj_name, off_t offset, off_t filesize);
  376. void
  377. replacement_file(const char* name, bool is_lib);
  378. void
  379. record_symbols(const Object* obj, int nsyms,
  380. const struct ld_plugin_symbol* syms);
  381. void
  382. finish()
  383. { ::fclose(this->logfile_); }
  384. private:
  385. unsigned int file_count_;
  386. const char* tempdir_;
  387. FILE* logfile_;
  388. };
  389. bool
  390. Plugin_recorder::init()
  391. {
  392. // Create a temporary directory where we can stash the log and
  393. // copies of replacement files.
  394. char dir_template[] = "gold-recording-XXXXXX";
  395. #ifdef HAVE_MKDTEMP
  396. if (mkdtemp(dir_template) == NULL)
  397. return false;
  398. #else
  399. if (mktemp(dir_template) == NULL)
  400. return false;
  401. #if defined (_WIN32) && !defined (__CYGWIN32__)
  402. if (mkdir(dir_template) != 0)
  403. return false;
  404. #else
  405. if (mkdir(dir_template, 0700) != 0)
  406. return false;
  407. #endif
  408. #endif
  409. size_t len = strlen(dir_template) + 1;
  410. char* tempdir = new char[len];
  411. memcpy(tempdir, dir_template, len);
  412. // Create the log file.
  413. std::string logname(tempdir);
  414. logname.append("/log");
  415. FILE* logfile = ::fopen(logname.c_str(), "w");
  416. if (logfile == NULL)
  417. return false;
  418. this->tempdir_ = tempdir;
  419. this->logfile_ = logfile;
  420. gold_info(_("%s: recording to %s"), program_name, this->tempdir_);
  421. return true;
  422. }
  423. void
  424. Plugin_recorder::claimed_file(const std::string& obj_name,
  425. off_t offset,
  426. off_t filesize,
  427. const std::string& plugin_name)
  428. {
  429. fprintf(this->logfile_, "PLUGIN: %s\n", plugin_name.c_str());
  430. fprintf(this->logfile_, "CLAIMED: %s", obj_name.c_str());
  431. if (offset > 0)
  432. fprintf(this->logfile_, " @%ld", static_cast<long>(offset));
  433. fprintf(this->logfile_, " %ld\n", static_cast<long>(filesize));
  434. }
  435. void
  436. Plugin_recorder::unclaimed_file(const std::string& obj_name,
  437. off_t offset,
  438. off_t filesize)
  439. {
  440. fprintf(this->logfile_, "UNCLAIMED: %s", obj_name.c_str());
  441. if (offset > 0)
  442. fprintf(this->logfile_, " @%ld", static_cast<long>(offset));
  443. fprintf(this->logfile_, " %ld\n", static_cast<long>(filesize));
  444. }
  445. // Make a hard link to INNAME from OUTNAME, if possible.
  446. // If not, copy the file.
  447. static bool
  448. link_or_copy_file(const char* inname, const char* outname)
  449. {
  450. static char buf[4096];
  451. #ifdef HAVE_LINK
  452. if (::link(inname, outname) == 0)
  453. return true;
  454. #endif
  455. int in = ::open(inname, O_RDONLY);
  456. if (in < 0)
  457. {
  458. gold_warning(_("%s: can't open (%s)"), inname, strerror(errno));
  459. return false;
  460. }
  461. int out = ::open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0600);
  462. if (out < 0)
  463. {
  464. gold_warning(_("%s: can't create (%s)"), outname, strerror(errno));
  465. ::close(in);
  466. return false;
  467. }
  468. ssize_t len;
  469. while ((len = ::read(in, buf, sizeof(buf))) > 0)
  470. {
  471. if (::write(out, buf, len) != len)
  472. {
  473. gold_warning(_("%s: write error while making copy of file (%s)"),
  474. inname, strerror(errno));
  475. break;
  476. }
  477. }
  478. ::close(in);
  479. ::close(out);
  480. return true;
  481. }
  482. void
  483. Plugin_recorder::replacement_file(const char* name, bool is_lib)
  484. {
  485. fprintf(this->logfile_, "REPLACEMENT: %s", name);
  486. if (is_lib)
  487. fprintf(this->logfile_, "(lib)");
  488. else
  489. {
  490. char counter[10];
  491. const char* basename = lbasename(name);
  492. snprintf(counter, sizeof(counter), "%05d", this->file_count_);
  493. ++this->file_count_;
  494. std::string outname(this->tempdir_);
  495. outname.append("/");
  496. outname.append(counter);
  497. outname.append("-");
  498. outname.append(basename);
  499. if (link_or_copy_file(name, outname.c_str()))
  500. fprintf(this->logfile_, " -> %s", outname.c_str());
  501. }
  502. fprintf(this->logfile_, "\n");
  503. }
  504. void
  505. Plugin_recorder::record_symbols(const Object* obj, int nsyms,
  506. const struct ld_plugin_symbol* syms)
  507. {
  508. fprintf(this->logfile_, "SYMBOLS: %d %s\n", nsyms, obj->name().c_str());
  509. for (int i = 0; i < nsyms; ++i)
  510. {
  511. const struct ld_plugin_symbol* isym = &syms[i];
  512. const char* def;
  513. switch (isym->def)
  514. {
  515. case LDPK_DEF:
  516. def = "D";
  517. break;
  518. case LDPK_WEAKDEF:
  519. def = "WD";
  520. break;
  521. case LDPK_UNDEF:
  522. def = "U";
  523. break;
  524. case LDPK_WEAKUNDEF:
  525. def = "WU";
  526. break;
  527. case LDPK_COMMON:
  528. def = "C";
  529. break;
  530. default:
  531. def = "?";
  532. break;
  533. }
  534. char vis;
  535. switch (isym->visibility)
  536. {
  537. case LDPV_PROTECTED:
  538. vis = 'P';
  539. break;
  540. case LDPV_INTERNAL:
  541. vis = 'I';
  542. break;
  543. case LDPV_HIDDEN:
  544. vis = 'H';
  545. break;
  546. case LDPV_DEFAULT:
  547. vis = 'D';
  548. break;
  549. default:
  550. vis = '?';
  551. break;
  552. }
  553. fprintf(this->logfile_, " %5d: %-2s %c %s", i, def, vis, isym->name);
  554. if (isym->version != NULL && isym->version[0] != '\0')
  555. fprintf(this->logfile_, "@%s", isym->version);
  556. if (isym->comdat_key != NULL && isym->comdat_key[0] != '\0')
  557. {
  558. if (strcmp(isym->name, isym->comdat_key) == 0)
  559. fprintf(this->logfile_, " [comdat]");
  560. else
  561. fprintf(this->logfile_, " [comdat: %s]", isym->comdat_key);
  562. }
  563. fprintf(this->logfile_, "\n");
  564. }
  565. }
  566. // Plugin_manager methods.
  567. Plugin_manager::~Plugin_manager()
  568. {
  569. for (Plugin_list::iterator p = this->plugins_.begin();
  570. p != this->plugins_.end();
  571. ++p)
  572. delete *p;
  573. this->plugins_.clear();
  574. for (Object_list::iterator obj = this->objects_.begin();
  575. obj != this->objects_.end();
  576. ++obj)
  577. delete *obj;
  578. this->objects_.clear();
  579. delete this->lock_;
  580. delete this->recorder_;
  581. }
  582. // Load all plugin libraries.
  583. void
  584. Plugin_manager::load_plugins(Layout* layout)
  585. {
  586. this->layout_ = layout;
  587. if (is_debugging_enabled(DEBUG_PLUGIN))
  588. {
  589. this->recorder_ = new Plugin_recorder();
  590. this->recorder_->init();
  591. }
  592. for (this->current_ = this->plugins_.begin();
  593. this->current_ != this->plugins_.end();
  594. ++this->current_)
  595. (*this->current_)->load();
  596. }
  597. // Call the plugin claim-file handlers in turn to see if any claim the file.
  598. Pluginobj*
  599. Plugin_manager::claim_file(Input_file* input_file, off_t offset,
  600. off_t filesize, Object* elf_object)
  601. {
  602. bool lock_initialized = this->initialize_lock_.initialize();
  603. gold_assert(lock_initialized);
  604. Hold_lock hl(*this->lock_);
  605. unsigned int handle = this->objects_.size();
  606. this->input_file_ = input_file;
  607. this->plugin_input_file_.name = input_file->filename().c_str();
  608. this->plugin_input_file_.fd = input_file->file().descriptor();
  609. this->plugin_input_file_.offset = offset;
  610. this->plugin_input_file_.filesize = filesize;
  611. this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
  612. if (elf_object != NULL)
  613. this->objects_.push_back(elf_object);
  614. this->in_claim_file_handler_ = true;
  615. for (Plugin_list::iterator p = this->plugins_.begin();
  616. p != this->plugins_.end();
  617. ++p)
  618. {
  619. // If we aren't yet in replacement phase, allow plugins to claim input
  620. // files, otherwise notify the plugin of the new input file, if needed.
  621. if (!this->in_replacement_phase_)
  622. {
  623. if ((*p)->claim_file(&this->plugin_input_file_))
  624. {
  625. this->any_claimed_ = true;
  626. this->in_claim_file_handler_ = false;
  627. if (this->recorder_ != NULL)
  628. {
  629. const std::string& objname = (elf_object == NULL
  630. ? input_file->filename()
  631. : elf_object->name());
  632. this->recorder_->claimed_file(objname,
  633. offset, filesize,
  634. (*p)->filename());
  635. }
  636. if (this->objects_.size() > handle
  637. && this->objects_[handle]->pluginobj() != NULL)
  638. return this->objects_[handle]->pluginobj();
  639. // If the plugin claimed the file but did not call the
  640. // add_symbols callback, we need to create the Pluginobj now.
  641. Pluginobj* obj = this->make_plugin_object(handle);
  642. return obj;
  643. }
  644. }
  645. else
  646. {
  647. (*p)->new_input(&this->plugin_input_file_);
  648. }
  649. }
  650. this->in_claim_file_handler_ = false;
  651. if (this->recorder_ != NULL)
  652. this->recorder_->unclaimed_file(input_file->filename(), offset, filesize);
  653. return NULL;
  654. }
  655. // Save an archive. This is used so that a plugin can add a file
  656. // which refers to a symbol which was not previously referenced. In
  657. // that case we want to pretend that the symbol was referenced before,
  658. // and pull in the archive object.
  659. void
  660. Plugin_manager::save_archive(Archive* archive)
  661. {
  662. if (this->in_replacement_phase_ || !this->any_claimed_)
  663. delete archive;
  664. else
  665. this->rescannable_.push_back(Rescannable(archive));
  666. }
  667. // Save an Input_group. This is like save_archive.
  668. void
  669. Plugin_manager::save_input_group(Input_group* input_group)
  670. {
  671. if (this->in_replacement_phase_ || !this->any_claimed_)
  672. delete input_group;
  673. else
  674. this->rescannable_.push_back(Rescannable(input_group));
  675. }
  676. // Call the all-symbols-read handlers.
  677. void
  678. Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
  679. Input_objects* input_objects,
  680. Symbol_table* symtab,
  681. Dirsearch* dirpath, Mapfile* mapfile,
  682. Task_token** last_blocker)
  683. {
  684. this->in_replacement_phase_ = true;
  685. this->workqueue_ = workqueue;
  686. this->task_ = task;
  687. this->input_objects_ = input_objects;
  688. this->symtab_ = symtab;
  689. this->dirpath_ = dirpath;
  690. this->mapfile_ = mapfile;
  691. this->this_blocker_ = NULL;
  692. // Set symbols used in defsym expressions as seen in real ELF.
  693. Layout *layout = parameters->options().plugins()->layout();
  694. layout->script_options()->set_defsym_uses_in_real_elf(symtab);
  695. layout->script_options()->find_defsym_defs(this->defsym_defines_set_);
  696. for (Plugin_list::iterator p = this->plugins_.begin();
  697. p != this->plugins_.end();
  698. ++p)
  699. (*p)->all_symbols_read();
  700. if (this->any_added_)
  701. {
  702. Task_token* next_blocker = new Task_token(true);
  703. next_blocker->add_blocker();
  704. workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
  705. this->this_blocker_ = next_blocker;
  706. }
  707. *last_blocker = this->this_blocker_;
  708. }
  709. // This is called when we see a new undefined symbol. If we are in
  710. // the replacement phase, this means that we may need to rescan some
  711. // archives we have previously seen.
  712. void
  713. Plugin_manager::new_undefined_symbol(Symbol* sym)
  714. {
  715. if (this->in_replacement_phase_)
  716. this->undefined_symbols_.push_back(sym);
  717. }
  718. // Rescan archives as needed. This handles the case where a new
  719. // object file added by a plugin has an undefined reference to some
  720. // symbol defined in an archive.
  721. void
  722. Plugin_manager::rescan(Task* task)
  723. {
  724. size_t rescan_pos = 0;
  725. size_t rescan_size = this->rescannable_.size();
  726. while (!this->undefined_symbols_.empty())
  727. {
  728. if (rescan_pos >= rescan_size)
  729. {
  730. this->undefined_symbols_.clear();
  731. return;
  732. }
  733. Undefined_symbol_list undefs;
  734. undefs.reserve(this->undefined_symbols_.size());
  735. this->undefined_symbols_.swap(undefs);
  736. size_t min_rescan_pos = rescan_size;
  737. for (Undefined_symbol_list::const_iterator p = undefs.begin();
  738. p != undefs.end();
  739. ++p)
  740. {
  741. if (!(*p)->is_undefined())
  742. continue;
  743. this->undefined_symbols_.push_back(*p);
  744. // Find the first rescan archive which defines this symbol,
  745. // starting at the current rescan position. The rescan position
  746. // exists so that given -la -lb -lc we don't look for undefined
  747. // symbols in -lb back in -la, but instead get the definition
  748. // from -lc. Don't bother to look past the current minimum
  749. // rescan position.
  750. for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
  751. {
  752. if (this->rescannable_defines(i, *p))
  753. {
  754. min_rescan_pos = i;
  755. break;
  756. }
  757. }
  758. }
  759. if (min_rescan_pos >= rescan_size)
  760. {
  761. // We didn't find any rescannable archives which define any
  762. // undefined symbols.
  763. return;
  764. }
  765. const Rescannable& r(this->rescannable_[min_rescan_pos]);
  766. if (r.is_archive)
  767. {
  768. Task_lock_obj<Archive> tl(task, r.u.archive);
  769. r.u.archive->add_symbols(this->symtab_, this->layout_,
  770. this->input_objects_, this->mapfile_);
  771. }
  772. else
  773. {
  774. size_t next_saw_undefined = this->symtab_->saw_undefined();
  775. size_t saw_undefined;
  776. do
  777. {
  778. saw_undefined = next_saw_undefined;
  779. for (Input_group::const_iterator p = r.u.input_group->begin();
  780. p != r.u.input_group->end();
  781. ++p)
  782. {
  783. Task_lock_obj<Archive> tl(task, *p);
  784. (*p)->add_symbols(this->symtab_, this->layout_,
  785. this->input_objects_, this->mapfile_);
  786. }
  787. next_saw_undefined = this->symtab_->saw_undefined();
  788. }
  789. while (saw_undefined != next_saw_undefined);
  790. }
  791. for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
  792. {
  793. if (this->rescannable_[i].is_archive)
  794. delete this->rescannable_[i].u.archive;
  795. else
  796. delete this->rescannable_[i].u.input_group;
  797. }
  798. rescan_pos = min_rescan_pos + 1;
  799. }
  800. }
  801. // Return whether the rescannable at index I defines SYM.
  802. bool
  803. Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
  804. {
  805. const Rescannable& r(this->rescannable_[i]);
  806. if (r.is_archive)
  807. return r.u.archive->defines_symbol(sym);
  808. else
  809. {
  810. for (Input_group::const_iterator p = r.u.input_group->begin();
  811. p != r.u.input_group->end();
  812. ++p)
  813. {
  814. if ((*p)->defines_symbol(sym))
  815. return true;
  816. }
  817. return false;
  818. }
  819. }
  820. // Layout deferred objects.
  821. void
  822. Plugin_manager::layout_deferred_objects()
  823. {
  824. Deferred_layout_list::iterator obj;
  825. for (obj = this->deferred_layout_objects_.begin();
  826. obj != this->deferred_layout_objects_.end();
  827. ++obj)
  828. {
  829. // Lock the object so we can read from it. This is only called
  830. // single-threaded from queue_middle_tasks, so it is OK to lock.
  831. // Unfortunately we have no way to pass in a Task token.
  832. const Task* dummy_task = reinterpret_cast<const Task*>(-1);
  833. Task_lock_obj<Object> tl(dummy_task, *obj);
  834. (*obj)->layout_deferred_sections(this->layout_);
  835. }
  836. }
  837. // Call the cleanup handlers.
  838. void
  839. Plugin_manager::cleanup()
  840. {
  841. if (this->any_added_)
  842. {
  843. // If any input files were added, close all the input files.
  844. // This is because the plugin may want to remove them, and on
  845. // Windows you are not allowed to remove an open file.
  846. close_all_descriptors();
  847. }
  848. for (Plugin_list::iterator p = this->plugins_.begin();
  849. p != this->plugins_.end();
  850. ++p)
  851. (*p)->cleanup();
  852. }
  853. // Make a new Pluginobj object. This is called when the plugin calls
  854. // the add_symbols API.
  855. Pluginobj*
  856. Plugin_manager::make_plugin_object(unsigned int handle)
  857. {
  858. // Make sure we aren't asked to make an object for the same handle twice.
  859. if (this->objects_.size() != handle
  860. && this->objects_[handle]->pluginobj() != NULL)
  861. return NULL;
  862. const std::string* filename = &this->input_file_->filename();
  863. // If the elf object for this file was pushed into the objects_ vector,
  864. // use its filename, then delete it to make room for the Pluginobj as
  865. // this file is claimed.
  866. if (this->objects_.size() != handle)
  867. {
  868. filename = &this->objects_.back()->name();
  869. this->objects_.pop_back();
  870. }
  871. Pluginobj* obj = make_sized_plugin_object(*filename,
  872. this->input_file_,
  873. this->plugin_input_file_.offset,
  874. this->plugin_input_file_.filesize);
  875. this->objects_.push_back(obj);
  876. return obj;
  877. }
  878. // Get the input file information with an open (possibly re-opened)
  879. // file descriptor.
  880. ld_plugin_status
  881. Plugin_manager::get_input_file(unsigned int handle,
  882. struct ld_plugin_input_file* file)
  883. {
  884. Pluginobj* obj = this->object(handle)->pluginobj();
  885. if (obj == NULL)
  886. return LDPS_BAD_HANDLE;
  887. obj->lock(this->task_);
  888. file->name = obj->filename().c_str();
  889. file->fd = obj->descriptor();
  890. file->offset = obj->offset();
  891. file->filesize = obj->filesize();
  892. file->handle = reinterpret_cast<void*>(handle);
  893. return LDPS_OK;
  894. }
  895. // Release the input file.
  896. ld_plugin_status
  897. Plugin_manager::release_input_file(unsigned int handle)
  898. {
  899. if (this->object(handle) == NULL)
  900. return LDPS_BAD_HANDLE;
  901. Pluginobj* obj = this->object(handle)->pluginobj();
  902. if (obj == NULL)
  903. return LDPS_BAD_HANDLE;
  904. obj->unlock(this->task_);
  905. return LDPS_OK;
  906. }
  907. // Get the elf object corresponding to the handle. Return NULL if we
  908. // found a Pluginobj instead.
  909. Object*
  910. Plugin_manager::get_elf_object(const void* handle)
  911. {
  912. Object* obj = this->object(
  913. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
  914. // The object should not be a Pluginobj.
  915. if (obj == NULL
  916. || obj->pluginobj() != NULL)
  917. return NULL;
  918. return obj;
  919. }
  920. ld_plugin_status
  921. Plugin_manager::get_view(unsigned int handle, const void **viewp)
  922. {
  923. off_t offset;
  924. size_t filesize;
  925. Input_file *input_file;
  926. if (this->in_claim_file_handler_)
  927. {
  928. // We are being called from the claim_file hook.
  929. const struct ld_plugin_input_file &f = this->plugin_input_file_;
  930. offset = f.offset;
  931. filesize = f.filesize;
  932. input_file = this->input_file_;
  933. }
  934. else
  935. {
  936. // An already claimed file.
  937. if (this->object(handle) == NULL)
  938. return LDPS_BAD_HANDLE;
  939. Pluginobj* obj = this->object(handle)->pluginobj();
  940. if (obj == NULL)
  941. return LDPS_BAD_HANDLE;
  942. offset = obj->offset();
  943. filesize = obj->filesize();
  944. input_file = obj->input_file();
  945. }
  946. *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
  947. false);
  948. return LDPS_OK;
  949. }
  950. // Add a new library path.
  951. ld_plugin_status
  952. Plugin_manager::set_extra_library_path(const char* path)
  953. {
  954. this->extra_search_path_ = std::string(path);
  955. return LDPS_OK;
  956. }
  957. // Add a new input file.
  958. ld_plugin_status
  959. Plugin_manager::add_input_file(const char* pathname, bool is_lib)
  960. {
  961. Input_file_argument file(pathname,
  962. (is_lib
  963. ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
  964. : Input_file_argument::INPUT_FILE_TYPE_FILE),
  965. (is_lib
  966. ? this->extra_search_path_.c_str()
  967. : ""),
  968. false,
  969. this->options_);
  970. Input_argument* input_argument = new Input_argument(file);
  971. Task_token* next_blocker = new Task_token(true);
  972. next_blocker->add_blocker();
  973. if (parameters->incremental())
  974. gold_error(_("input files added by plug-ins in --incremental mode not "
  975. "supported yet"));
  976. if (this->recorder_ != NULL)
  977. this->recorder_->replacement_file(pathname, is_lib);
  978. this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
  979. this->symtab_,
  980. this->layout_,
  981. this->dirpath_,
  982. 0,
  983. this->mapfile_,
  984. input_argument,
  985. NULL,
  986. NULL,
  987. this->this_blocker_,
  988. next_blocker));
  989. this->this_blocker_ = next_blocker;
  990. this->any_added_ = true;
  991. return LDPS_OK;
  992. }
  993. // Class Pluginobj.
  994. Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
  995. off_t offset, off_t filesize)
  996. : Object(name, input_file, false, offset),
  997. nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
  998. {
  999. }
  1000. // Return TRUE if a defined symbol is referenced from outside the
  1001. // universe of claimed objects. Only references from relocatable,
  1002. // non-IR (unclaimed) objects count as a reference. References from
  1003. // dynamic objects count only as "visible".
  1004. static inline bool
  1005. is_referenced_from_outside(Symbol* lsym)
  1006. {
  1007. if (lsym->in_real_elf())
  1008. return true;
  1009. if (parameters->options().relocatable())
  1010. return true;
  1011. if (parameters->options().is_undefined(lsym->name()))
  1012. return true;
  1013. return false;
  1014. }
  1015. // Return TRUE if a defined symbol might be reachable from outside the
  1016. // load module.
  1017. static inline bool
  1018. is_visible_from_outside(Symbol* lsym)
  1019. {
  1020. if (lsym->in_dyn())
  1021. return true;
  1022. if (parameters->options().export_dynamic() || parameters->options().shared()
  1023. || parameters->options().in_dynamic_list(lsym->name())
  1024. || parameters->options().is_export_dynamic_symbol(lsym->name()))
  1025. return lsym->is_externally_visible();
  1026. return false;
  1027. }
  1028. // Get symbol resolution info.
  1029. ld_plugin_status
  1030. Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
  1031. int nsyms,
  1032. ld_plugin_symbol* syms,
  1033. int version) const
  1034. {
  1035. // For version 1 of this interface, we cannot use
  1036. // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
  1037. // instead.
  1038. const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
  1039. = (version > 1
  1040. ? LDPR_PREVAILING_DEF_IRONLY_EXP
  1041. : LDPR_PREVAILING_DEF);
  1042. if (nsyms > this->nsyms_)
  1043. return LDPS_NO_SYMS;
  1044. if (static_cast<size_t>(nsyms) > this->symbols_.size())
  1045. {
  1046. // We never decided to include this object. We mark all symbols as
  1047. // preempted.
  1048. gold_assert(this->symbols_.size() == 0);
  1049. for (int i = 0; i < nsyms; i++)
  1050. syms[i].resolution = LDPR_PREEMPTED_REG;
  1051. return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
  1052. }
  1053. Plugin_manager* plugins = parameters->options().plugins();
  1054. for (int i = 0; i < nsyms; i++)
  1055. {
  1056. ld_plugin_symbol* isym = &syms[i];
  1057. Symbol* lsym = this->symbols_[i];
  1058. if (lsym->is_forwarder())
  1059. lsym = symtab->resolve_forwards(lsym);
  1060. ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
  1061. if (plugins->is_defsym_def(lsym->name()))
  1062. {
  1063. // The symbol is redefined via defsym.
  1064. res = LDPR_PREEMPTED_REG;
  1065. }
  1066. else if (lsym->is_undefined())
  1067. {
  1068. // The symbol remains undefined.
  1069. res = LDPR_UNDEF;
  1070. }
  1071. else if (isym->def == LDPK_UNDEF
  1072. || isym->def == LDPK_WEAKUNDEF
  1073. || isym->def == LDPK_COMMON)
  1074. {
  1075. // The original symbol was undefined or common.
  1076. if (lsym->source() != Symbol::FROM_OBJECT)
  1077. res = LDPR_RESOLVED_EXEC;
  1078. else if (lsym->object()->pluginobj() == this)
  1079. {
  1080. if (is_referenced_from_outside(lsym))
  1081. res = LDPR_PREVAILING_DEF;
  1082. else if (is_visible_from_outside(lsym))
  1083. res = ldpr_prevailing_def_ironly_exp;
  1084. else
  1085. res = LDPR_PREVAILING_DEF_IRONLY;
  1086. }
  1087. else if (lsym->object()->pluginobj() != NULL)
  1088. res = LDPR_RESOLVED_IR;
  1089. else if (lsym->object()->is_dynamic())
  1090. res = LDPR_RESOLVED_DYN;
  1091. else
  1092. res = LDPR_RESOLVED_EXEC;
  1093. }
  1094. else
  1095. {
  1096. // The original symbol was a definition.
  1097. if (lsym->source() != Symbol::FROM_OBJECT)
  1098. res = LDPR_PREEMPTED_REG;
  1099. else if (lsym->object() == static_cast<const Object*>(this))
  1100. {
  1101. if (is_referenced_from_outside(lsym))
  1102. res = LDPR_PREVAILING_DEF;
  1103. else if (is_visible_from_outside(lsym))
  1104. res = ldpr_prevailing_def_ironly_exp;
  1105. else
  1106. res = LDPR_PREVAILING_DEF_IRONLY;
  1107. }
  1108. else
  1109. res = (lsym->object()->pluginobj() != NULL
  1110. ? LDPR_PREEMPTED_IR
  1111. : LDPR_PREEMPTED_REG);
  1112. }
  1113. isym->resolution = res;
  1114. }
  1115. return LDPS_OK;
  1116. }
  1117. // Return TRUE if the comdat group with key COMDAT_KEY from this object
  1118. // should be kept.
  1119. bool
  1120. Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
  1121. {
  1122. std::pair<Comdat_map::iterator, bool> ins =
  1123. this->comdat_map_.insert(std::make_pair(comdat_key, false));
  1124. // If this is the first time we've seen this comdat key, ask the
  1125. // layout object whether it should be included.
  1126. if (ins.second)
  1127. ins.first->second = layout->find_or_add_kept_section(comdat_key,
  1128. NULL, 0, true,
  1129. true, NULL);
  1130. return ins.first->second;
  1131. }
  1132. // Class Sized_pluginobj.
  1133. template<int size, bool big_endian>
  1134. Sized_pluginobj<size, big_endian>::Sized_pluginobj(
  1135. const std::string& name,
  1136. Input_file* input_file,
  1137. off_t offset,
  1138. off_t filesize)
  1139. : Pluginobj(name, input_file, offset, filesize)
  1140. {
  1141. }
  1142. // Read the symbols. Not used for plugin objects.
  1143. template<int size, bool big_endian>
  1144. void
  1145. Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
  1146. {
  1147. gold_unreachable();
  1148. }
  1149. // Lay out the input sections. Not used for plugin objects.
  1150. template<int size, bool big_endian>
  1151. void
  1152. Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
  1153. Read_symbols_data*)
  1154. {
  1155. gold_unreachable();
  1156. }
  1157. // Add the symbols to the symbol table.
  1158. template<int size, bool big_endian>
  1159. void
  1160. Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
  1161. Read_symbols_data*,
  1162. Layout* layout)
  1163. {
  1164. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1165. unsigned char symbuf[sym_size];
  1166. elfcpp::Sym_write<size, big_endian> osym(symbuf);
  1167. Plugin_recorder* recorder = parameters->options().plugins()->recorder();
  1168. if (recorder != NULL)
  1169. recorder->record_symbols(this, this->nsyms_, this->syms_);
  1170. this->symbols_.resize(this->nsyms_);
  1171. for (int i = 0; i < this->nsyms_; ++i)
  1172. {
  1173. const struct ld_plugin_symbol* isym = &this->syms_[i];
  1174. const char* name = isym->name;
  1175. const char* ver = isym->version;
  1176. elfcpp::Elf_Half shndx;
  1177. elfcpp::STB bind;
  1178. elfcpp::STV vis;
  1179. if (name != NULL && name[0] == '\0')
  1180. name = NULL;
  1181. if (ver != NULL && ver[0] == '\0')
  1182. ver = NULL;
  1183. switch (isym->def)
  1184. {
  1185. case LDPK_WEAKDEF:
  1186. case LDPK_WEAKUNDEF:
  1187. bind = elfcpp::STB_WEAK;
  1188. break;
  1189. case LDPK_DEF:
  1190. case LDPK_UNDEF:
  1191. case LDPK_COMMON:
  1192. default:
  1193. bind = elfcpp::STB_GLOBAL;
  1194. break;
  1195. }
  1196. switch (isym->def)
  1197. {
  1198. case LDPK_DEF:
  1199. case LDPK_WEAKDEF:
  1200. // We use an arbitrary section number for a defined symbol.
  1201. shndx = 1;
  1202. break;
  1203. case LDPK_COMMON:
  1204. shndx = elfcpp::SHN_COMMON;
  1205. break;
  1206. case LDPK_UNDEF:
  1207. case LDPK_WEAKUNDEF:
  1208. default:
  1209. shndx = elfcpp::SHN_UNDEF;
  1210. break;
  1211. }
  1212. switch (isym->visibility)
  1213. {
  1214. case LDPV_PROTECTED:
  1215. vis = elfcpp::STV_PROTECTED;
  1216. break;
  1217. case LDPV_INTERNAL:
  1218. vis = elfcpp::STV_INTERNAL;
  1219. break;
  1220. case LDPV_HIDDEN:
  1221. vis = elfcpp::STV_HIDDEN;
  1222. break;
  1223. case LDPV_DEFAULT:
  1224. default:
  1225. vis = elfcpp::STV_DEFAULT;
  1226. break;
  1227. }
  1228. if (isym->comdat_key != NULL
  1229. && isym->comdat_key[0] != '\0'
  1230. && !this->include_comdat_group(isym->comdat_key, layout))
  1231. shndx = elfcpp::SHN_UNDEF;
  1232. osym.put_st_name(0);
  1233. osym.put_st_value(0);
  1234. osym.put_st_size(0);
  1235. osym.put_st_info(bind, elfcpp::STT_NOTYPE);
  1236. osym.put_st_other(vis, 0);
  1237. osym.put_st_shndx(shndx);
  1238. elfcpp::Sym<size, big_endian> sym(symbuf);
  1239. this->symbols_[i] =
  1240. symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
  1241. }
  1242. }
  1243. template<int size, bool big_endian>
  1244. Archive::Should_include
  1245. Sized_pluginobj<size, big_endian>::do_should_include_member(
  1246. Symbol_table* symtab,
  1247. Layout* layout,
  1248. Read_symbols_data*,
  1249. std::string* why)
  1250. {
  1251. char* tmpbuf = NULL;
  1252. size_t tmpbuflen = 0;
  1253. for (int i = 0; i < this->nsyms_; ++i)
  1254. {
  1255. const struct ld_plugin_symbol& sym = this->syms_[i];
  1256. if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
  1257. continue;
  1258. const char* name = sym.name;
  1259. Symbol* symbol;
  1260. Archive::Should_include t = Archive::should_include_member(symtab,
  1261. layout,
  1262. name,
  1263. &symbol, why,
  1264. &tmpbuf,
  1265. &tmpbuflen);
  1266. if (t == Archive::SHOULD_INCLUDE_YES)
  1267. {
  1268. if (tmpbuf != NULL)
  1269. free(tmpbuf);
  1270. return t;
  1271. }
  1272. }
  1273. if (tmpbuf != NULL)
  1274. free(tmpbuf);
  1275. return Archive::SHOULD_INCLUDE_UNKNOWN;
  1276. }
  1277. // Iterate over global symbols, calling a visitor class V for each.
  1278. template<int size, bool big_endian>
  1279. void
  1280. Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
  1281. Read_symbols_data*,
  1282. Library_base::Symbol_visitor_base* v)
  1283. {
  1284. for (int i = 0; i < this->nsyms_; ++i)
  1285. {
  1286. const struct ld_plugin_symbol& sym = this->syms_[i];
  1287. if (sym.def != LDPK_UNDEF)
  1288. v->visit(sym.name);
  1289. }
  1290. }
  1291. // Iterate over local symbols, calling a visitor class V for each GOT offset
  1292. // associated with a local symbol.
  1293. template<int size, bool big_endian>
  1294. void
  1295. Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
  1296. Got_offset_list::Visitor*) const
  1297. {
  1298. gold_unreachable();
  1299. }
  1300. // Get the size of a section. Not used for plugin objects.
  1301. template<int size, bool big_endian>
  1302. uint64_t
  1303. Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
  1304. {
  1305. gold_unreachable();
  1306. return 0;
  1307. }
  1308. // Get the name of a section. Not used for plugin objects.
  1309. template<int size, bool big_endian>
  1310. std::string
  1311. Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
  1312. {
  1313. gold_unreachable();
  1314. return std::string();
  1315. }
  1316. // Return a view of the contents of a section. Not used for plugin objects.
  1317. template<int size, bool big_endian>
  1318. const unsigned char*
  1319. Sized_pluginobj<size, big_endian>::do_section_contents(
  1320. unsigned int,
  1321. section_size_type*,
  1322. bool)
  1323. {
  1324. gold_unreachable();
  1325. return NULL;
  1326. }
  1327. // Return section flags. Not used for plugin objects.
  1328. template<int size, bool big_endian>
  1329. uint64_t
  1330. Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
  1331. {
  1332. gold_unreachable();
  1333. return 0;
  1334. }
  1335. // Return section entsize. Not used for plugin objects.
  1336. template<int size, bool big_endian>
  1337. uint64_t
  1338. Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
  1339. {
  1340. gold_unreachable();
  1341. return 0;
  1342. }
  1343. // Return section address. Not used for plugin objects.
  1344. template<int size, bool big_endian>
  1345. uint64_t
  1346. Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
  1347. {
  1348. gold_unreachable();
  1349. return 0;
  1350. }
  1351. // Return section type. Not used for plugin objects.
  1352. template<int size, bool big_endian>
  1353. unsigned int
  1354. Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
  1355. {
  1356. gold_unreachable();
  1357. return 0;
  1358. }
  1359. // Return the section link field. Not used for plugin objects.
  1360. template<int size, bool big_endian>
  1361. unsigned int
  1362. Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
  1363. {
  1364. gold_unreachable();
  1365. return 0;
  1366. }
  1367. // Return the section link field. Not used for plugin objects.
  1368. template<int size, bool big_endian>
  1369. unsigned int
  1370. Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
  1371. {
  1372. gold_unreachable();
  1373. return 0;
  1374. }
  1375. // Return the section alignment. Not used for plugin objects.
  1376. template<int size, bool big_endian>
  1377. uint64_t
  1378. Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
  1379. {
  1380. gold_unreachable();
  1381. return 0;
  1382. }
  1383. // Return the Xindex structure to use. Not used for plugin objects.
  1384. template<int size, bool big_endian>
  1385. Xindex*
  1386. Sized_pluginobj<size, big_endian>::do_initialize_xindex()
  1387. {
  1388. gold_unreachable();
  1389. return NULL;
  1390. }
  1391. // Get symbol counts. Don't count plugin objects; the replacement
  1392. // files will provide the counts.
  1393. template<int size, bool big_endian>
  1394. void
  1395. Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
  1396. const Symbol_table*,
  1397. size_t* defined,
  1398. size_t* used) const
  1399. {
  1400. *defined = 0;
  1401. *used = 0;
  1402. }
  1403. // Get symbols. Not used for plugin objects.
  1404. template<int size, bool big_endian>
  1405. const Object::Symbols*
  1406. Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
  1407. {
  1408. gold_unreachable();
  1409. }
  1410. // Class Plugin_finish. This task runs after all replacement files have
  1411. // been added. For now, it's a placeholder for a possible plugin API
  1412. // to allow the plugin to release most of its resources. The cleanup
  1413. // handlers must be called later, because they can remove the temporary
  1414. // object files that are needed until the end of the link.
  1415. class Plugin_finish : public Task
  1416. {
  1417. public:
  1418. Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
  1419. : this_blocker_(this_blocker), next_blocker_(next_blocker)
  1420. { }
  1421. ~Plugin_finish()
  1422. {
  1423. if (this->this_blocker_ != NULL)
  1424. delete this->this_blocker_;
  1425. }
  1426. Task_token*
  1427. is_runnable()
  1428. {
  1429. if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
  1430. return this->this_blocker_;
  1431. return NULL;
  1432. }
  1433. void
  1434. locks(Task_locker* tl)
  1435. { tl->add(this, this->next_blocker_); }
  1436. void
  1437. run(Workqueue*)
  1438. {
  1439. Plugin_manager* plugins = parameters->options().plugins();
  1440. gold_assert(plugins != NULL);
  1441. // We could call early cleanup handlers here.
  1442. if (plugins->recorder())
  1443. plugins->recorder()->finish();
  1444. }
  1445. std::string
  1446. get_name() const
  1447. { return "Plugin_finish"; }
  1448. private:
  1449. Task_token* this_blocker_;
  1450. Task_token* next_blocker_;
  1451. };
  1452. // Class Plugin_hook.
  1453. Plugin_hook::~Plugin_hook()
  1454. {
  1455. }
  1456. // Return whether a Plugin_hook task is runnable.
  1457. Task_token*
  1458. Plugin_hook::is_runnable()
  1459. {
  1460. if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
  1461. return this->this_blocker_;
  1462. return NULL;
  1463. }
  1464. // Return a Task_locker for a Plugin_hook task. We don't need any
  1465. // locks here.
  1466. void
  1467. Plugin_hook::locks(Task_locker*)
  1468. {
  1469. }
  1470. // Run the "all symbols read" plugin hook.
  1471. void
  1472. Plugin_hook::run(Workqueue* workqueue)
  1473. {
  1474. gold_assert(this->options_.has_plugins());
  1475. Symbol* start_sym = this->symtab_->lookup(parameters->entry());
  1476. if (start_sym != NULL)
  1477. start_sym->set_in_real_elf();
  1478. this->options_.plugins()->all_symbols_read(workqueue,
  1479. this,
  1480. this->input_objects_,
  1481. this->symtab_,
  1482. this->dirpath_,
  1483. this->mapfile_,
  1484. &this->this_blocker_);
  1485. workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
  1486. this->next_blocker_));
  1487. }
  1488. // The C interface routines called by the plugins.
  1489. #ifdef ENABLE_PLUGINS
  1490. // Register a claim-file handler.
  1491. static enum ld_plugin_status
  1492. register_claim_file(ld_plugin_claim_file_handler handler)
  1493. {
  1494. gold_assert(parameters->options().has_plugins());
  1495. parameters->options().plugins()->set_claim_file_handler(handler);
  1496. return LDPS_OK;
  1497. }
  1498. // Register an all-symbols-read handler.
  1499. static enum ld_plugin_status
  1500. register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
  1501. {
  1502. gold_assert(parameters->options().has_plugins());
  1503. parameters->options().plugins()->set_all_symbols_read_handler(handler);
  1504. return LDPS_OK;
  1505. }
  1506. // Register a cleanup handler.
  1507. static enum ld_plugin_status
  1508. register_cleanup(ld_plugin_cleanup_handler handler)
  1509. {
  1510. gold_assert(parameters->options().has_plugins());
  1511. parameters->options().plugins()->set_cleanup_handler(handler);
  1512. return LDPS_OK;
  1513. }
  1514. // Add symbols from a plugin-claimed input file.
  1515. static enum ld_plugin_status
  1516. add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
  1517. {
  1518. gold_assert(parameters->options().has_plugins());
  1519. Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
  1520. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
  1521. if (obj == NULL)
  1522. return LDPS_ERR;
  1523. obj->store_incoming_symbols(nsyms, syms);
  1524. return LDPS_OK;
  1525. }
  1526. // Get the input file information with an open (possibly re-opened)
  1527. // file descriptor.
  1528. static enum ld_plugin_status
  1529. get_input_file(const void* handle, struct ld_plugin_input_file* file)
  1530. {
  1531. gold_assert(parameters->options().has_plugins());
  1532. unsigned int obj_index =
  1533. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
  1534. return parameters->options().plugins()->get_input_file(obj_index, file);
  1535. }
  1536. // Release the input file.
  1537. static enum ld_plugin_status
  1538. release_input_file(const void* handle)
  1539. {
  1540. gold_assert(parameters->options().has_plugins());
  1541. unsigned int obj_index =
  1542. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
  1543. return parameters->options().plugins()->release_input_file(obj_index);
  1544. }
  1545. static enum ld_plugin_status
  1546. get_view(const void *handle, const void **viewp)
  1547. {
  1548. gold_assert(parameters->options().has_plugins());
  1549. unsigned int obj_index =
  1550. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
  1551. return parameters->options().plugins()->get_view(obj_index, viewp);
  1552. }
  1553. // Get the symbol resolution info for a plugin-claimed input file.
  1554. static enum ld_plugin_status
  1555. get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
  1556. {
  1557. gold_assert(parameters->options().has_plugins());
  1558. Plugin_manager* plugins = parameters->options().plugins();
  1559. Object* obj = plugins->object(
  1560. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
  1561. if (obj == NULL)
  1562. return LDPS_ERR;
  1563. Pluginobj* plugin_obj = obj->pluginobj();
  1564. if (plugin_obj == NULL)
  1565. return LDPS_ERR;
  1566. Symbol_table* symtab = plugins->symtab();
  1567. return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
  1568. }
  1569. // Version 2 of the above. The only difference is that this version
  1570. // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
  1571. static enum ld_plugin_status
  1572. get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
  1573. {
  1574. gold_assert(parameters->options().has_plugins());
  1575. Plugin_manager* plugins = parameters->options().plugins();
  1576. Object* obj = plugins->object(
  1577. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
  1578. if (obj == NULL)
  1579. return LDPS_ERR;
  1580. Pluginobj* plugin_obj = obj->pluginobj();
  1581. if (plugin_obj == NULL)
  1582. return LDPS_ERR;
  1583. Symbol_table* symtab = plugins->symtab();
  1584. return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
  1585. }
  1586. // Version 3 of the above. The only difference from v2 is that it
  1587. // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
  1588. // decided to include.
  1589. static enum ld_plugin_status
  1590. get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
  1591. {
  1592. gold_assert(parameters->options().has_plugins());
  1593. Plugin_manager* plugins = parameters->options().plugins();
  1594. Object* obj = plugins->object(
  1595. static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
  1596. if (obj == NULL)
  1597. return LDPS_ERR;
  1598. Pluginobj* plugin_obj = obj->pluginobj();
  1599. if (plugin_obj == NULL)
  1600. return LDPS_ERR;
  1601. Symbol_table* symtab = plugins->symtab();
  1602. return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
  1603. }
  1604. // Add a new (real) input file generated by a plugin.
  1605. static enum ld_plugin_status
  1606. add_input_file(const char* pathname)
  1607. {
  1608. gold_assert(parameters->options().has_plugins());
  1609. return parameters->options().plugins()->add_input_file(pathname, false);
  1610. }
  1611. // Add a new (real) library required by a plugin.
  1612. static enum ld_plugin_status
  1613. add_input_library(const char* pathname)
  1614. {
  1615. gold_assert(parameters->options().has_plugins());
  1616. return parameters->options().plugins()->add_input_file(pathname, true);
  1617. }
  1618. // Set the extra library path to be used by libraries added via
  1619. // add_input_library
  1620. static enum ld_plugin_status
  1621. set_extra_library_path(const char* path)
  1622. {
  1623. gold_assert(parameters->options().has_plugins());
  1624. return parameters->options().plugins()->set_extra_library_path(path);
  1625. }
  1626. // Issue a diagnostic message from a plugin.
  1627. static enum ld_plugin_status
  1628. message(int level, const char* format, ...)
  1629. {
  1630. va_list args;
  1631. va_start(args, format);
  1632. switch (level)
  1633. {
  1634. case LDPL_INFO:
  1635. parameters->errors()->info(format, args);
  1636. break;
  1637. case LDPL_WARNING:
  1638. parameters->errors()->warning(format, args);
  1639. break;
  1640. case LDPL_ERROR:
  1641. default:
  1642. parameters->errors()->error(format, args);
  1643. break;
  1644. case LDPL_FATAL:
  1645. parameters->errors()->fatal(format, args);
  1646. break;
  1647. }
  1648. va_end(args);
  1649. return LDPS_OK;
  1650. }
  1651. // Get the section count of the object corresponding to the handle. This
  1652. // plugin interface can only be called in the claim_file handler of the plugin.
  1653. static enum ld_plugin_status
  1654. get_input_section_count(const void* handle, unsigned int* count)
  1655. {
  1656. gold_assert(parameters->options().has_plugins());
  1657. if (!parameters->options().plugins()->in_claim_file_handler())
  1658. return LDPS_ERR;
  1659. Object* obj = parameters->options().plugins()->get_elf_object(handle);
  1660. if (obj == NULL)
  1661. return LDPS_ERR;
  1662. *count = obj->shnum();
  1663. return LDPS_OK;
  1664. }
  1665. // Get the type of the specified section in the object corresponding
  1666. // to the handle. This plugin interface can only be called in the
  1667. // claim_file handler of the plugin.
  1668. static enum ld_plugin_status
  1669. get_input_section_type(const struct ld_plugin_section section,
  1670. unsigned int* type)
  1671. {
  1672. gold_assert(parameters->options().has_plugins());
  1673. if (!parameters->options().plugins()->in_claim_file_handler())
  1674. return LDPS_ERR;
  1675. Object* obj
  1676. = parameters->options().plugins()->get_elf_object(section.handle);
  1677. if (obj == NULL)
  1678. return LDPS_BAD_HANDLE;
  1679. *type = obj->section_type(section.shndx);
  1680. return LDPS_OK;
  1681. }
  1682. // Get the name of the specified section in the object corresponding
  1683. // to the handle. This plugin interface can only be called in the
  1684. // claim_file handler of the plugin.
  1685. static enum ld_plugin_status
  1686. get_input_section_name(const struct ld_plugin_section section,
  1687. char** section_name_ptr)
  1688. {
  1689. gold_assert(parameters->options().has_plugins());
  1690. if (!parameters->options().plugins()->in_claim_file_handler())
  1691. return LDPS_ERR;
  1692. Object* obj
  1693. = parameters->options().plugins()->get_elf_object(section.handle);
  1694. if (obj == NULL)
  1695. return LDPS_BAD_HANDLE;
  1696. // Check if the object is locked before getting the section name.
  1697. gold_assert(obj->is_locked());
  1698. const std::string section_name = obj->section_name(section.shndx);
  1699. *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
  1700. memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
  1701. return LDPS_OK;
  1702. }
  1703. // Get the contents of the specified section in the object corresponding
  1704. // to the handle. This plugin interface can only be called in the
  1705. // claim_file handler of the plugin.
  1706. static enum ld_plugin_status
  1707. get_input_section_contents(const struct ld_plugin_section section,
  1708. const unsigned char** section_contents_ptr,
  1709. size_t* len)
  1710. {
  1711. gold_assert(parameters->options().has_plugins());
  1712. if (!parameters->options().plugins()->in_claim_file_handler())
  1713. return LDPS_ERR;
  1714. Object* obj
  1715. = parameters->options().plugins()->get_elf_object(section.handle);
  1716. if (obj == NULL)
  1717. return LDPS_BAD_HANDLE;
  1718. // Check if the object is locked before getting the section contents.
  1719. gold_assert(obj->is_locked());
  1720. section_size_type plen;
  1721. *section_contents_ptr
  1722. = obj->section_contents(section.shndx, &plen, false);
  1723. *len = plen;
  1724. return LDPS_OK;
  1725. }
  1726. // Get the alignment of the specified section in the object corresponding
  1727. // to the handle. This plugin interface can only be called in the
  1728. // claim_file handler of the plugin.
  1729. static enum ld_plugin_status
  1730. get_input_section_alignment(const struct ld_plugin_section section,
  1731. unsigned int* addralign)
  1732. {
  1733. gold_assert(parameters->options().has_plugins());
  1734. if (!parameters->options().plugins()->in_claim_file_handler())
  1735. return LDPS_ERR;
  1736. Object* obj
  1737. = parameters->options().plugins()->get_elf_object(section.handle);
  1738. if (obj == NULL)
  1739. return LDPS_BAD_HANDLE;
  1740. *addralign = obj->section_addralign(section.shndx);
  1741. return LDPS_OK;
  1742. }
  1743. // Get the size of the specified section in the object corresponding
  1744. // to the handle. This plugin interface can only be called in the
  1745. // claim_file handler of the plugin.
  1746. static enum ld_plugin_status
  1747. get_input_section_size(const struct ld_plugin_section section,
  1748. uint64_t* secsize)
  1749. {
  1750. gold_assert(parameters->options().has_plugins());
  1751. if (!parameters->options().plugins()->in_claim_file_handler())
  1752. return LDPS_ERR;
  1753. Object* obj
  1754. = parameters->options().plugins()->get_elf_object(section.handle);
  1755. if (obj == NULL)
  1756. return LDPS_BAD_HANDLE;
  1757. *secsize = obj->section_size(section.shndx);
  1758. return LDPS_OK;
  1759. }
  1760. static enum ld_plugin_status
  1761. get_wrap_symbols(uint64_t *count, const char ***wrap_symbols)
  1762. {
  1763. gold_assert(parameters->options().has_plugins());
  1764. *count = parameters->options().wrap_size();
  1765. if (*count == 0)
  1766. return LDPS_OK;
  1767. *wrap_symbols = new const char *[*count];
  1768. int i = 0;
  1769. for (options::String_set::const_iterator
  1770. it = parameters->options().wrap_begin();
  1771. it != parameters->options().wrap_end(); ++it, ++i) {
  1772. (*wrap_symbols)[i] = it->c_str();
  1773. }
  1774. return LDPS_OK;
  1775. }
  1776. // Specify the ordering of sections in the final layout. The sections are
  1777. // specified as (handle,shndx) pairs in the two arrays in the order in
  1778. // which they should appear in the final layout.
  1779. static enum ld_plugin_status
  1780. update_section_order(const struct ld_plugin_section* section_list,
  1781. unsigned int num_sections)
  1782. {
  1783. gold_assert(parameters->options().has_plugins());
  1784. if (num_sections == 0)
  1785. return LDPS_OK;
  1786. if (section_list == NULL)
  1787. return LDPS_ERR;
  1788. Layout* layout = parameters->options().plugins()->layout();
  1789. gold_assert (layout != NULL);
  1790. std::map<Section_id, unsigned int>* order_map
  1791. = layout->get_section_order_map();
  1792. /* Store the mapping from Section_id to section position in layout's
  1793. order_map to consult after output sections are added. */
  1794. for (unsigned int i = 0; i < num_sections; ++i)
  1795. {
  1796. Object* obj = parameters->options().plugins()->get_elf_object(
  1797. section_list[i].handle);
  1798. if (obj == NULL || obj->is_dynamic())
  1799. return LDPS_BAD_HANDLE;
  1800. unsigned int shndx = section_list[i].shndx;
  1801. Section_id secn_id(static_cast<Relobj*>(obj), shndx);
  1802. (*order_map)[secn_id] = i + 1;
  1803. }
  1804. return LDPS_OK;
  1805. }
  1806. // Let the linker know that the sections could be reordered.
  1807. static enum ld_plugin_status
  1808. allow_section_ordering()
  1809. {
  1810. gold_assert(parameters->options().has_plugins());
  1811. Layout* layout = parameters->options().plugins()->layout();
  1812. layout->set_section_ordering_specified();
  1813. return LDPS_OK;
  1814. }
  1815. // Let the linker know that a subset of sections could be mapped
  1816. // to a unique segment.
  1817. static enum ld_plugin_status
  1818. allow_unique_segment_for_sections()
  1819. {
  1820. gold_assert(parameters->options().has_plugins());
  1821. Layout* layout = parameters->options().plugins()->layout();
  1822. layout->set_unique_segment_for_sections_specified();
  1823. return LDPS_OK;
  1824. }
  1825. // This function should map the list of sections specified in the
  1826. // SECTION_LIST to a unique segment. ELF segments do not have names
  1827. // and the NAME is used to identify Output Section which should contain
  1828. // the list of sections. This Output Section will then be mapped to
  1829. // a unique segment. FLAGS is used to specify if any additional segment
  1830. // flags need to be set. For instance, a specific segment flag can be
  1831. // set to identify this segment. Unsetting segment flags is not possible.
  1832. // ALIGN specifies the alignment of the segment.
  1833. static enum ld_plugin_status
  1834. unique_segment_for_sections(const char* segment_name,
  1835. uint64_t flags,
  1836. uint64_t align,
  1837. const struct ld_plugin_section* section_list,
  1838. unsigned int num_sections)
  1839. {
  1840. gold_assert(parameters->options().has_plugins());
  1841. if (num_sections == 0)
  1842. return LDPS_OK;
  1843. if (section_list == NULL)
  1844. return LDPS_ERR;
  1845. Layout* layout = parameters->options().plugins()->layout();
  1846. gold_assert (layout != NULL);
  1847. Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
  1848. s->name = segment_name;
  1849. s->flags = flags;
  1850. s->align = align;
  1851. for (unsigned int i = 0; i < num_sections; ++i)
  1852. {
  1853. Object* obj = parameters->options().plugins()->get_elf_object(
  1854. section_list[i].handle);
  1855. if (obj == NULL || obj->is_dynamic())
  1856. return LDPS_BAD_HANDLE;
  1857. unsigned int shndx = section_list[i].shndx;
  1858. Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
  1859. layout->insert_section_segment_map(secn_id, s);
  1860. }
  1861. return LDPS_OK;
  1862. }
  1863. // Register a new_input handler.
  1864. static enum ld_plugin_status
  1865. register_new_input(ld_plugin_new_input_handler handler)
  1866. {
  1867. gold_assert(parameters->options().has_plugins());
  1868. parameters->options().plugins()->set_new_input_handler(handler);
  1869. return LDPS_OK;
  1870. }
  1871. #endif // ENABLE_PLUGINS
  1872. // Allocate a Pluginobj object of the appropriate size and endianness.
  1873. static Pluginobj*
  1874. make_sized_plugin_object(const std::string& filename,
  1875. Input_file* input_file, off_t offset, off_t filesize)
  1876. {
  1877. Pluginobj* obj = NULL;
  1878. parameters_force_valid_target();
  1879. const Target& target(parameters->target());
  1880. if (target.get_size() == 32)
  1881. {
  1882. if (target.is_big_endian())
  1883. #ifdef HAVE_TARGET_32_BIG
  1884. obj = new Sized_pluginobj<32, true>(filename, input_file,
  1885. offset, filesize);
  1886. #else
  1887. gold_error(_("%s: not configured to support "
  1888. "32-bit big-endian object"),
  1889. filename.c_str());
  1890. #endif
  1891. else
  1892. #ifdef HAVE_TARGET_32_LITTLE
  1893. obj = new Sized_pluginobj<32, false>(filename, input_file,
  1894. offset, filesize);
  1895. #else
  1896. gold_error(_("%s: not configured to support "
  1897. "32-bit little-endian object"),
  1898. filename.c_str());
  1899. #endif
  1900. }
  1901. else if (target.get_size() == 64)
  1902. {
  1903. if (target.is_big_endian())
  1904. #ifdef HAVE_TARGET_64_BIG
  1905. obj = new Sized_pluginobj<64, true>(filename, input_file,
  1906. offset, filesize);
  1907. #else
  1908. gold_error(_("%s: not configured to support "
  1909. "64-bit big-endian object"),
  1910. filename.c_str());
  1911. #endif
  1912. else
  1913. #ifdef HAVE_TARGET_64_LITTLE
  1914. obj = new Sized_pluginobj<64, false>(filename, input_file,
  1915. offset, filesize);
  1916. #else
  1917. gold_error(_("%s: not configured to support "
  1918. "64-bit little-endian object"),
  1919. filename.c_str());
  1920. #endif
  1921. }
  1922. gold_assert(obj != NULL);
  1923. return obj;
  1924. }
  1925. } // End namespace gold.