collect2.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  1. /* Collect static initialization info into data structures that can be
  2. traversed by C++ initialization and finalization routines.
  3. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  4. Contributed by Chris Smith (csmith@convex.com).
  5. Heavily modified by Michael Meissner (meissner@cygnus.com),
  6. Per Bothner (bothner@cygnus.com), and John Gilmore (gnu@cygnus.com).
  7. This file is part of GCC.
  8. GCC is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 3, or (at your option) any later
  11. version.
  12. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with GCC; see the file COPYING3. If not see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Build tables of static constructors and destructors and run ld. */
  20. #include "config.h"
  21. #include "system.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "filenames.h"
  25. #include "file-find.h"
  26. /* TARGET_64BIT may be defined to use driver specific functionality. */
  27. #undef TARGET_64BIT
  28. #define TARGET_64BIT TARGET_64BIT_DEFAULT
  29. #ifndef LIBRARY_PATH_ENV
  30. #define LIBRARY_PATH_ENV "LIBRARY_PATH"
  31. #endif
  32. #define COLLECT
  33. #include "collect2.h"
  34. #include "collect2-aix.h"
  35. #include "collect-utils.h"
  36. #include "diagnostic.h"
  37. #include "demangle.h"
  38. #include "obstack.h"
  39. #include "intl.h"
  40. #include "version.h"
  41. /* On certain systems, we have code that works by scanning the object file
  42. directly. But this code uses system-specific header files and library
  43. functions, so turn it off in a cross-compiler. Likewise, the names of
  44. the utilities are not correct for a cross-compiler; we have to hope that
  45. cross-versions are in the proper directories. */
  46. #ifdef CROSS_DIRECTORY_STRUCTURE
  47. #ifndef CROSS_AIX_SUPPORT
  48. #undef OBJECT_FORMAT_COFF
  49. #endif
  50. #undef MD_EXEC_PREFIX
  51. #undef REAL_LD_FILE_NAME
  52. #undef REAL_NM_FILE_NAME
  53. #undef REAL_STRIP_FILE_NAME
  54. #endif
  55. /* If we cannot use a special method, use the ordinary one:
  56. run nm to find what symbols are present.
  57. In a cross-compiler, this means you need a cross nm,
  58. but that is not quite as unpleasant as special headers. */
  59. #if !defined (OBJECT_FORMAT_COFF)
  60. #define OBJECT_FORMAT_NONE
  61. #endif
  62. #ifdef OBJECT_FORMAT_COFF
  63. #ifndef CROSS_DIRECTORY_STRUCTURE
  64. #include <a.out.h>
  65. #include <ar.h>
  66. #ifdef UMAX
  67. #include <sgs.h>
  68. #endif
  69. /* Many versions of ldfcn.h define these. */
  70. #ifdef FREAD
  71. #undef FREAD
  72. #undef FWRITE
  73. #endif
  74. #include <ldfcn.h>
  75. #endif
  76. /* Some systems have an ISCOFF macro, but others do not. In some cases
  77. the macro may be wrong. MY_ISCOFF is defined in tm.h files for machines
  78. that either do not have an ISCOFF macro in /usr/include or for those
  79. where it is wrong. */
  80. #ifndef MY_ISCOFF
  81. #define MY_ISCOFF(X) ISCOFF (X)
  82. #endif
  83. #endif /* OBJECT_FORMAT_COFF */
  84. #ifdef OBJECT_FORMAT_NONE
  85. /* Default flags to pass to nm. */
  86. #ifndef NM_FLAGS
  87. #define NM_FLAGS "-n"
  88. #endif
  89. #endif /* OBJECT_FORMAT_NONE */
  90. /* Some systems use __main in a way incompatible with its use in gcc, in these
  91. cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
  92. give the same symbol without quotes for an alternative entry point. */
  93. #ifndef NAME__MAIN
  94. #define NAME__MAIN "__main"
  95. #endif
  96. /* This must match tree.h. */
  97. #define DEFAULT_INIT_PRIORITY 65535
  98. #ifndef COLLECT_SHARED_INIT_FUNC
  99. #define COLLECT_SHARED_INIT_FUNC(STREAM, FUNC) \
  100. fprintf ((STREAM), "void _GLOBAL__DI() {\n\t%s();\n}\n", (FUNC))
  101. #endif
  102. #ifndef COLLECT_SHARED_FINI_FUNC
  103. #define COLLECT_SHARED_FINI_FUNC(STREAM, FUNC) \
  104. fprintf ((STREAM), "void _GLOBAL__DD() {\n\t%s();\n}\n", (FUNC))
  105. #endif
  106. #ifdef LDD_SUFFIX
  107. #define SCAN_LIBRARIES
  108. #endif
  109. #ifndef SHLIB_SUFFIX
  110. #define SHLIB_SUFFIX ".so"
  111. #endif
  112. #ifdef USE_COLLECT2
  113. int do_collecting = 1;
  114. #else
  115. int do_collecting = 0;
  116. #endif
  117. /* Cook up an always defined indication of whether we proceed the
  118. "EXPORT_LIST" way. */
  119. #ifdef COLLECT_EXPORT_LIST
  120. #define DO_COLLECT_EXPORT_LIST 1
  121. #else
  122. #define DO_COLLECT_EXPORT_LIST 0
  123. #endif
  124. /* Nonzero if we should suppress the automatic demangling of identifiers
  125. in linker error messages. Set from COLLECT_NO_DEMANGLE. */
  126. int no_demangle;
  127. /* Linked lists of constructor and destructor names. */
  128. struct id
  129. {
  130. struct id *next;
  131. int sequence;
  132. char name[1];
  133. };
  134. struct head
  135. {
  136. struct id *first;
  137. struct id *last;
  138. int number;
  139. };
  140. static int rflag; /* true if -r */
  141. static int strip_flag; /* true if -s */
  142. #ifdef COLLECT_EXPORT_LIST
  143. static int export_flag; /* true if -bE */
  144. static int aix64_flag; /* true if -b64 */
  145. static int aixrtl_flag; /* true if -brtl */
  146. static int aixlazy_flag; /* true if -blazy */
  147. #endif
  148. enum lto_mode_d {
  149. LTO_MODE_NONE, /* Not doing LTO. */
  150. LTO_MODE_LTO, /* Normal LTO. */
  151. LTO_MODE_WHOPR /* WHOPR. */
  152. };
  153. /* Current LTO mode. */
  154. #ifdef ENABLE_LTO
  155. static enum lto_mode_d lto_mode = LTO_MODE_WHOPR;
  156. #else
  157. static enum lto_mode_d lto_mode = LTO_MODE_NONE;
  158. #endif
  159. bool helpflag; /* true if --help */
  160. static int shared_obj; /* true if -shared */
  161. static const char *c_file; /* <xxx>.c for constructor/destructor list. */
  162. static const char *o_file; /* <xxx>.o for constructor/destructor list. */
  163. #ifdef COLLECT_EXPORT_LIST
  164. static const char *export_file; /* <xxx>.x for AIX export list. */
  165. #endif
  166. static char **lto_o_files; /* Output files for LTO. */
  167. const char *ldout; /* File for ld stdout. */
  168. const char *lderrout; /* File for ld stderr. */
  169. static const char *output_file; /* Output file for ld. */
  170. static const char *nm_file_name; /* pathname of nm */
  171. #ifdef LDD_SUFFIX
  172. static const char *ldd_file_name; /* pathname of ldd (or equivalent) */
  173. #endif
  174. static const char *strip_file_name; /* pathname of strip */
  175. const char *c_file_name; /* pathname of gcc */
  176. static char *initname, *fininame; /* names of init and fini funcs */
  177. #ifdef TARGET_AIX_VERSION
  178. static char *aix_shared_initname;
  179. static char *aix_shared_fininame; /* init/fini names as per the scheme
  180. described in config/rs6000/aix.h */
  181. #endif
  182. static struct head constructors; /* list of constructors found */
  183. static struct head destructors; /* list of destructors found */
  184. #ifdef COLLECT_EXPORT_LIST
  185. static struct head exports; /* list of exported symbols */
  186. #endif
  187. static struct head frame_tables; /* list of frame unwind info tables */
  188. bool at_file_supplied; /* Whether to use @file arguments */
  189. struct obstack temporary_obstack;
  190. char * temporary_firstobj;
  191. /* A string that must be prepended to a target OS path in order to find
  192. it on the host system. */
  193. #ifdef TARGET_SYSTEM_ROOT
  194. static const char *target_system_root = TARGET_SYSTEM_ROOT;
  195. #else
  196. static const char *target_system_root = "";
  197. #endif
  198. /* Whether we may unlink the output file, which should be set as soon as we
  199. know we have successfully produced it. This is typically useful to prevent
  200. blindly attempting to unlink a read-only output that the target linker
  201. would leave untouched. */
  202. bool may_unlink_output_file = false;
  203. #ifdef COLLECT_EXPORT_LIST
  204. /* Lists to keep libraries to be scanned for global constructors/destructors. */
  205. static struct head libs; /* list of libraries */
  206. static struct path_prefix cmdline_lib_dirs; /* directories specified with -L */
  207. static struct path_prefix libpath_lib_dirs; /* directories in LIBPATH */
  208. static struct path_prefix *libpaths[3] = {&cmdline_lib_dirs,
  209. &libpath_lib_dirs, NULL};
  210. #endif
  211. /* List of names of object files containing LTO information.
  212. These are a subset of the object file names appearing on the
  213. command line, and must be identical, in the sense of pointer
  214. equality, with the names passed to maybe_run_lto_and_relink(). */
  215. struct lto_object
  216. {
  217. const char *name; /* Name of object file. */
  218. struct lto_object *next; /* Next in linked list. */
  219. };
  220. struct lto_object_list
  221. {
  222. struct lto_object *first; /* First list element. */
  223. struct lto_object *last; /* Last list element. */
  224. };
  225. static struct lto_object_list lto_objects;
  226. /* Special kinds of symbols that a name may denote. */
  227. typedef enum {
  228. SYM_REGULAR = 0, /* nothing special */
  229. SYM_CTOR = 1, /* constructor */
  230. SYM_DTOR = 2, /* destructor */
  231. SYM_INIT = 3, /* shared object routine that calls all the ctors */
  232. SYM_FINI = 4, /* shared object routine that calls all the dtors */
  233. SYM_DWEH = 5, /* DWARF exception handling table */
  234. SYM_AIXI = 6,
  235. SYM_AIXD = 7
  236. } symkind;
  237. const char tool_name[] = "collect2";
  238. static symkind is_ctor_dtor (const char *);
  239. static void handler (int);
  240. static void maybe_unlink_list (char **);
  241. static void add_to_list (struct head *, const char *);
  242. static int extract_init_priority (const char *);
  243. static void sort_ids (struct head *);
  244. static void write_list (FILE *, const char *, struct id *);
  245. #ifdef COLLECT_EXPORT_LIST
  246. static void dump_list (FILE *, const char *, struct id *);
  247. #endif
  248. #if 0
  249. static void dump_prefix_list (FILE *, const char *, struct prefix_list *);
  250. #endif
  251. static void write_list_with_asm (FILE *, const char *, struct id *);
  252. static void write_c_file (FILE *, const char *);
  253. static void write_c_file_stat (FILE *, const char *);
  254. #ifndef LD_INIT_SWITCH
  255. static void write_c_file_glob (FILE *, const char *);
  256. #endif
  257. #ifdef SCAN_LIBRARIES
  258. static void scan_libraries (const char *);
  259. #endif
  260. #ifdef COLLECT_EXPORT_LIST
  261. #if 0
  262. static int is_in_list (const char *, struct id *);
  263. #endif
  264. static void write_aix_file (FILE *, struct id *);
  265. static char *resolve_lib_name (const char *);
  266. #endif
  267. static char *extract_string (const char **);
  268. static void post_ld_pass (bool);
  269. static void process_args (int *argcp, char **argv);
  270. /* Enumerations describing which pass this is for scanning the
  271. program file ... */
  272. typedef enum {
  273. PASS_FIRST, /* without constructors */
  274. PASS_OBJ, /* individual objects */
  275. PASS_LIB, /* looking for shared libraries */
  276. PASS_SECOND, /* with constructors linked in */
  277. PASS_LTOINFO /* looking for objects with LTO info */
  278. } scanpass;
  279. /* ... and which kinds of symbols are to be considered. */
  280. enum scanfilter_masks {
  281. SCAN_NOTHING = 0,
  282. SCAN_CTOR = 1 << SYM_CTOR,
  283. SCAN_DTOR = 1 << SYM_DTOR,
  284. SCAN_INIT = 1 << SYM_INIT,
  285. SCAN_FINI = 1 << SYM_FINI,
  286. SCAN_DWEH = 1 << SYM_DWEH,
  287. SCAN_AIXI = 1 << SYM_AIXI,
  288. SCAN_AIXD = 1 << SYM_AIXD,
  289. SCAN_ALL = ~0
  290. };
  291. /* This type is used for parameters and variables which hold
  292. combinations of the flags in enum scanfilter_masks. */
  293. typedef int scanfilter;
  294. /* Scan the name list of the loaded program for the symbols g++ uses for
  295. static constructors and destructors.
  296. The SCANPASS argument tells which collect processing pass this is for and
  297. the SCANFILTER argument tells which kinds of symbols to consider in this
  298. pass. Symbols of a special kind not in the filter mask are considered as
  299. regular ones.
  300. The constructor table begins at __CTOR_LIST__ and contains a count of the
  301. number of pointers (or -1 if the constructors are built in a separate
  302. section by the linker), followed by the pointers to the constructor
  303. functions, terminated with a null pointer. The destructor table has the
  304. same format, and begins at __DTOR_LIST__. */
  305. static void scan_prog_file (const char *, scanpass, scanfilter);
  306. /* Delete tempfiles and exit function. */
  307. void
  308. tool_cleanup (bool from_signal)
  309. {
  310. if (c_file != 0 && c_file[0])
  311. maybe_unlink (c_file);
  312. if (o_file != 0 && o_file[0])
  313. maybe_unlink (o_file);
  314. #ifdef COLLECT_EXPORT_LIST
  315. if (export_file != 0 && export_file[0])
  316. maybe_unlink (export_file);
  317. #endif
  318. if (lto_o_files)
  319. maybe_unlink_list (lto_o_files);
  320. if (ldout != 0 && ldout[0])
  321. {
  322. if (!from_signal)
  323. dump_ld_file (ldout, stdout);
  324. maybe_unlink (ldout);
  325. }
  326. if (lderrout != 0 && lderrout[0])
  327. {
  328. if (!from_signal)
  329. dump_ld_file (lderrout, stderr);
  330. maybe_unlink (lderrout);
  331. }
  332. }
  333. static void
  334. collect_atexit (void)
  335. {
  336. tool_cleanup (false);
  337. }
  338. static void
  339. handler (int signo)
  340. {
  341. tool_cleanup (true);
  342. signal (signo, SIG_DFL);
  343. raise (signo);
  344. }
  345. /* Notify user of a non-error, without translating the format string. */
  346. void
  347. notice_translated (const char *cmsgid, ...)
  348. {
  349. va_list ap;
  350. va_start (ap, cmsgid);
  351. vfprintf (stderr, cmsgid, ap);
  352. va_end (ap);
  353. }
  354. int
  355. file_exists (const char *name)
  356. {
  357. return access (name, R_OK) == 0;
  358. }
  359. /* Parse a reasonable subset of shell quoting syntax. */
  360. static char *
  361. extract_string (const char **pp)
  362. {
  363. const char *p = *pp;
  364. int backquote = 0;
  365. int inside = 0;
  366. for (;;)
  367. {
  368. char c = *p;
  369. if (c == '\0')
  370. break;
  371. ++p;
  372. if (backquote)
  373. obstack_1grow (&temporary_obstack, c);
  374. else if (! inside && c == ' ')
  375. break;
  376. else if (! inside && c == '\\')
  377. backquote = 1;
  378. else if (c == '\'')
  379. inside = !inside;
  380. else
  381. obstack_1grow (&temporary_obstack, c);
  382. }
  383. obstack_1grow (&temporary_obstack, '\0');
  384. *pp = p;
  385. return XOBFINISH (&temporary_obstack, char *);
  386. }
  387. void
  388. dump_ld_file (const char *name, FILE *to)
  389. {
  390. FILE *stream = fopen (name, "r");
  391. if (stream == 0)
  392. return;
  393. while (1)
  394. {
  395. int c;
  396. while (c = getc (stream),
  397. c != EOF && (ISIDNUM (c) || c == '$' || c == '.'))
  398. obstack_1grow (&temporary_obstack, c);
  399. if (obstack_object_size (&temporary_obstack) > 0)
  400. {
  401. const char *word, *p;
  402. char *result;
  403. obstack_1grow (&temporary_obstack, '\0');
  404. word = XOBFINISH (&temporary_obstack, const char *);
  405. if (*word == '.')
  406. ++word, putc ('.', to);
  407. p = word;
  408. if (!strncmp (p, USER_LABEL_PREFIX, strlen (USER_LABEL_PREFIX)))
  409. p += strlen (USER_LABEL_PREFIX);
  410. #ifdef HAVE_LD_DEMANGLE
  411. result = 0;
  412. #else
  413. if (no_demangle)
  414. result = 0;
  415. else
  416. result = cplus_demangle (p, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
  417. #endif
  418. if (result)
  419. {
  420. int diff;
  421. fputs (result, to);
  422. diff = strlen (word) - strlen (result);
  423. while (diff > 0 && c == ' ')
  424. --diff, putc (' ', to);
  425. if (diff < 0 && c == ' ')
  426. {
  427. while (diff < 0 && c == ' ')
  428. ++diff, c = getc (stream);
  429. if (!ISSPACE (c))
  430. {
  431. /* Make sure we output at least one space, or
  432. the demangled symbol name will run into
  433. whatever text follows. */
  434. putc (' ', to);
  435. }
  436. }
  437. free (result);
  438. }
  439. else
  440. fputs (word, to);
  441. fflush (to);
  442. obstack_free (&temporary_obstack, temporary_firstobj);
  443. }
  444. if (c == EOF)
  445. break;
  446. putc (c, to);
  447. }
  448. fclose (stream);
  449. }
  450. /* Return the kind of symbol denoted by name S. */
  451. static symkind
  452. is_ctor_dtor (const char *s)
  453. {
  454. struct names { const char *const name; const int len; symkind ret;
  455. const int two_underscores; };
  456. const struct names *p;
  457. int ch;
  458. const char *orig_s = s;
  459. static const struct names special[] = {
  460. #ifndef NO_DOLLAR_IN_LABEL
  461. { "GLOBAL__I$", sizeof ("GLOBAL__I$")-1, SYM_CTOR, 0 },
  462. { "GLOBAL__D$", sizeof ("GLOBAL__D$")-1, SYM_DTOR, 0 },
  463. #else
  464. #ifndef NO_DOT_IN_LABEL
  465. { "GLOBAL__I.", sizeof ("GLOBAL__I.")-1, SYM_CTOR, 0 },
  466. { "GLOBAL__D.", sizeof ("GLOBAL__D.")-1, SYM_DTOR, 0 },
  467. #endif /* NO_DOT_IN_LABEL */
  468. #endif /* NO_DOLLAR_IN_LABEL */
  469. { "GLOBAL__I_", sizeof ("GLOBAL__I_")-1, SYM_CTOR, 0 },
  470. { "GLOBAL__D_", sizeof ("GLOBAL__D_")-1, SYM_DTOR, 0 },
  471. { "GLOBAL__F_", sizeof ("GLOBAL__F_")-1, SYM_DWEH, 0 },
  472. { "GLOBAL__FI_", sizeof ("GLOBAL__FI_")-1, SYM_INIT, 0 },
  473. { "GLOBAL__FD_", sizeof ("GLOBAL__FD_")-1, SYM_FINI, 0 },
  474. #ifdef TARGET_AIX_VERSION
  475. { "GLOBAL__AIXI_", sizeof ("GLOBAL__AIXI_")-1, SYM_AIXI, 0 },
  476. { "GLOBAL__AIXD_", sizeof ("GLOBAL__AIXD_")-1, SYM_AIXD, 0 },
  477. #endif
  478. { NULL, 0, SYM_REGULAR, 0 }
  479. };
  480. while ((ch = *s) == '_')
  481. ++s;
  482. if (s == orig_s)
  483. return SYM_REGULAR;
  484. for (p = &special[0]; p->len > 0; p++)
  485. {
  486. if (ch == p->name[0]
  487. && (!p->two_underscores || ((s - orig_s) >= 2))
  488. && strncmp (s, p->name, p->len) == 0)
  489. {
  490. return p->ret;
  491. }
  492. }
  493. return SYM_REGULAR;
  494. }
  495. /* We maintain two prefix lists: one from COMPILER_PATH environment variable
  496. and one from the PATH variable. */
  497. static struct path_prefix cpath, path;
  498. #ifdef CROSS_DIRECTORY_STRUCTURE
  499. /* This is the name of the target machine. We use it to form the name
  500. of the files to execute. */
  501. static const char *const target_machine = TARGET_MACHINE;
  502. #endif
  503. /* Search for NAME using prefix list PPREFIX. We only look for executable
  504. files.
  505. Return 0 if not found, otherwise return its name, allocated with malloc. */
  506. #ifdef OBJECT_FORMAT_NONE
  507. /* Add an entry for the object file NAME to object file list LIST.
  508. New entries are added at the end of the list. The original pointer
  509. value of NAME is preserved, i.e., no string copy is performed. */
  510. static void
  511. add_lto_object (struct lto_object_list *list, const char *name)
  512. {
  513. struct lto_object *n = XNEW (struct lto_object);
  514. n->name = name;
  515. n->next = NULL;
  516. if (list->last)
  517. list->last->next = n;
  518. else
  519. list->first = n;
  520. list->last = n;
  521. }
  522. #endif /* OBJECT_FORMAT_NONE */
  523. /* Perform a link-time recompilation and relink if any of the object
  524. files contain LTO info. The linker command line LTO_LD_ARGV
  525. represents the linker command that would produce a final executable
  526. without the use of LTO. OBJECT_LST is a vector of object file names
  527. appearing in LTO_LD_ARGV that are to be considered for link-time
  528. recompilation, where OBJECT is a pointer to the last valid element.
  529. (This awkward convention avoids an impedance mismatch with the
  530. usage of similarly-named variables in main().) The elements of
  531. OBJECT_LST must be identical, i.e., pointer equal, to the
  532. corresponding arguments in LTO_LD_ARGV.
  533. Upon entry, at least one linker run has been performed without the
  534. use of any LTO info that might be present. Any recompilations
  535. necessary for template instantiations have been performed, and
  536. initializer/finalizer tables have been created if needed and
  537. included in the linker command line LTO_LD_ARGV. If any of the
  538. object files contain LTO info, we run the LTO back end on all such
  539. files, and perform the final link with the LTO back end output
  540. substituted for the LTO-optimized files. In some cases, a final
  541. link with all link-time generated code has already been performed,
  542. so there is no need to relink if no LTO info is found. In other
  543. cases, our caller has not produced the final executable, and is
  544. relying on us to perform the required link whether LTO info is
  545. present or not. In that case, the FORCE argument should be true.
  546. Note that the linker command line argument LTO_LD_ARGV passed into
  547. this function may be modified in place. */
  548. static void
  549. maybe_run_lto_and_relink (char **lto_ld_argv, char **object_lst,
  550. const char **object, bool force)
  551. {
  552. const char **object_file = CONST_CAST2 (const char **, char **, object_lst);
  553. int num_lto_c_args = 1; /* Allow space for the terminating NULL. */
  554. while (object_file < object)
  555. {
  556. /* If file contains LTO info, add it to the list of LTO objects. */
  557. scan_prog_file (*object_file++, PASS_LTOINFO, SCAN_ALL);
  558. /* Increment the argument count by the number of object file arguments
  559. we will add. An upper bound suffices, so just count all of the
  560. object files regardless of whether they contain LTO info. */
  561. num_lto_c_args++;
  562. }
  563. if (lto_objects.first)
  564. {
  565. char **lto_c_argv;
  566. const char **lto_c_ptr;
  567. char **p;
  568. char **lto_o_ptr;
  569. struct lto_object *list;
  570. char *lto_wrapper = getenv ("COLLECT_LTO_WRAPPER");
  571. struct pex_obj *pex;
  572. const char *prog = "lto-wrapper";
  573. int lto_ld_argv_size = 0;
  574. char **out_lto_ld_argv;
  575. int out_lto_ld_argv_size;
  576. size_t num_files;
  577. if (!lto_wrapper)
  578. fatal_error (input_location, "COLLECT_LTO_WRAPPER must be set");
  579. num_lto_c_args++;
  580. /* There is at least one object file containing LTO info,
  581. so we need to run the LTO back end and relink.
  582. To do so we build updated ld arguments with first
  583. LTO object replaced by all partitions and other LTO
  584. objects removed. */
  585. lto_c_argv = (char **) xcalloc (sizeof (char *), num_lto_c_args);
  586. lto_c_ptr = CONST_CAST2 (const char **, char **, lto_c_argv);
  587. *lto_c_ptr++ = lto_wrapper;
  588. /* Add LTO objects to the wrapper command line. */
  589. for (list = lto_objects.first; list; list = list->next)
  590. *lto_c_ptr++ = list->name;
  591. *lto_c_ptr = NULL;
  592. /* Run the LTO back end. */
  593. pex = collect_execute (prog, lto_c_argv, NULL, NULL, PEX_SEARCH,
  594. at_file_supplied);
  595. {
  596. int c;
  597. FILE *stream;
  598. size_t i;
  599. char *start, *end;
  600. stream = pex_read_output (pex, 0);
  601. gcc_assert (stream);
  602. num_files = 0;
  603. while ((c = getc (stream)) != EOF)
  604. {
  605. obstack_1grow (&temporary_obstack, c);
  606. if (c == '\n')
  607. ++num_files;
  608. }
  609. lto_o_files = XNEWVEC (char *, num_files + 1);
  610. lto_o_files[num_files] = NULL;
  611. start = XOBFINISH (&temporary_obstack, char *);
  612. for (i = 0; i < num_files; ++i)
  613. {
  614. end = start;
  615. while (*end != '\n')
  616. ++end;
  617. *end = '\0';
  618. lto_o_files[i] = xstrdup (start);
  619. start = end + 1;
  620. }
  621. obstack_free (&temporary_obstack, temporary_firstobj);
  622. }
  623. do_wait (prog, pex);
  624. pex = NULL;
  625. /* Compute memory needed for new LD arguments. At most number of original arguemtns
  626. plus number of partitions. */
  627. for (lto_ld_argv_size = 0; lto_ld_argv[lto_ld_argv_size]; lto_ld_argv_size++)
  628. ;
  629. out_lto_ld_argv = XCNEWVEC (char *, num_files + lto_ld_argv_size + 1);
  630. out_lto_ld_argv_size = 0;
  631. /* After running the LTO back end, we will relink, substituting
  632. the LTO output for the object files that we submitted to the
  633. LTO. Here, we modify the linker command line for the relink. */
  634. /* Copy all arguments until we find first LTO file. */
  635. p = lto_ld_argv;
  636. while (*p != NULL)
  637. {
  638. for (list = lto_objects.first; list; list = list->next)
  639. if (*p == list->name) /* Note test for pointer equality! */
  640. break;
  641. if (list)
  642. break;
  643. out_lto_ld_argv[out_lto_ld_argv_size++] = *p++;
  644. }
  645. /* Now insert all LTO partitions. */
  646. lto_o_ptr = lto_o_files;
  647. while (*lto_o_ptr)
  648. out_lto_ld_argv[out_lto_ld_argv_size++] = *lto_o_ptr++;
  649. /* ... and copy the rest. */
  650. while (*p != NULL)
  651. {
  652. for (list = lto_objects.first; list; list = list->next)
  653. if (*p == list->name) /* Note test for pointer equality! */
  654. break;
  655. if (!list)
  656. out_lto_ld_argv[out_lto_ld_argv_size++] = *p;
  657. p++;
  658. }
  659. out_lto_ld_argv[out_lto_ld_argv_size++] = 0;
  660. /* Run the linker again, this time replacing the object files
  661. optimized by the LTO with the temporary file generated by the LTO. */
  662. fork_execute ("ld", out_lto_ld_argv, HAVE_GNU_LD && at_file_supplied);
  663. post_ld_pass (true);
  664. free (lto_ld_argv);
  665. maybe_unlink_list (lto_o_files);
  666. }
  667. else if (force)
  668. {
  669. /* Our caller is relying on us to do the link
  670. even though there is no LTO back end work to be done. */
  671. fork_execute ("ld", lto_ld_argv, HAVE_GNU_LD && at_file_supplied);
  672. post_ld_pass (false);
  673. }
  674. else
  675. post_ld_pass (true);
  676. }
  677. /* Main program. */
  678. int
  679. main (int argc, char **argv)
  680. {
  681. enum linker_select
  682. {
  683. USE_DEFAULT_LD,
  684. USE_PLUGIN_LD,
  685. USE_GOLD_LD,
  686. USE_BFD_LD,
  687. USE_LD_MAX
  688. } selected_linker = USE_DEFAULT_LD;
  689. static const char *const ld_suffixes[USE_LD_MAX] =
  690. {
  691. "ld",
  692. PLUGIN_LD_SUFFIX,
  693. "ld.gold",
  694. "ld.bfd"
  695. };
  696. static const char *const real_ld_suffix = "real-ld";
  697. static const char *const collect_ld_suffix = "collect-ld";
  698. static const char *const nm_suffix = "nm";
  699. static const char *const gnm_suffix = "gnm";
  700. #ifdef LDD_SUFFIX
  701. static const char *const ldd_suffix = LDD_SUFFIX;
  702. #endif
  703. static const char *const strip_suffix = "strip";
  704. static const char *const gstrip_suffix = "gstrip";
  705. const char *full_ld_suffixes[USE_LD_MAX];
  706. #ifdef CROSS_DIRECTORY_STRUCTURE
  707. /* If we look for a program in the compiler directories, we just use
  708. the short name, since these directories are already system-specific.
  709. But it we look for a program in the system directories, we need to
  710. qualify the program name with the target machine. */
  711. const char *const full_nm_suffix =
  712. concat (target_machine, "-", nm_suffix, NULL);
  713. const char *const full_gnm_suffix =
  714. concat (target_machine, "-", gnm_suffix, NULL);
  715. #ifdef LDD_SUFFIX
  716. const char *const full_ldd_suffix =
  717. concat (target_machine, "-", ldd_suffix, NULL);
  718. #endif
  719. const char *const full_strip_suffix =
  720. concat (target_machine, "-", strip_suffix, NULL);
  721. const char *const full_gstrip_suffix =
  722. concat (target_machine, "-", gstrip_suffix, NULL);
  723. #else
  724. #ifdef LDD_SUFFIX
  725. const char *const full_ldd_suffix = ldd_suffix;
  726. #endif
  727. const char *const full_nm_suffix = nm_suffix;
  728. const char *const full_gnm_suffix = gnm_suffix;
  729. const char *const full_strip_suffix = strip_suffix;
  730. const char *const full_gstrip_suffix = gstrip_suffix;
  731. #endif /* CROSS_DIRECTORY_STRUCTURE */
  732. const char *arg;
  733. FILE *outf;
  734. #ifdef COLLECT_EXPORT_LIST
  735. FILE *exportf;
  736. #endif
  737. const char *ld_file_name;
  738. const char *p;
  739. char **c_argv;
  740. const char **c_ptr;
  741. char **ld1_argv;
  742. const char **ld1;
  743. bool use_plugin = false;
  744. bool use_collect_ld = false;
  745. /* The kinds of symbols we will have to consider when scanning the
  746. outcome of a first pass link. This is ALL to start with, then might
  747. be adjusted before getting to the first pass link per se, typically on
  748. AIX where we perform an early scan of objects and libraries to fetch
  749. the list of global ctors/dtors and make sure they are not garbage
  750. collected. */
  751. scanfilter ld1_filter = SCAN_ALL;
  752. char **ld2_argv;
  753. const char **ld2;
  754. char **object_lst;
  755. const char **object;
  756. #ifdef TARGET_AIX_VERSION
  757. int object_nbr = argc;
  758. #endif
  759. int first_file;
  760. int num_c_args;
  761. char **old_argv;
  762. int i;
  763. for (i = 0; i < USE_LD_MAX; i++)
  764. full_ld_suffixes[i]
  765. #ifdef CROSS_DIRECTORY_STRUCTURE
  766. = concat (target_machine, "-", ld_suffixes[i], NULL);
  767. #else
  768. = ld_suffixes[i];
  769. #endif
  770. p = argv[0] + strlen (argv[0]);
  771. while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
  772. --p;
  773. progname = p;
  774. xmalloc_set_program_name (progname);
  775. old_argv = argv;
  776. expandargv (&argc, &argv);
  777. if (argv != old_argv)
  778. at_file_supplied = 1;
  779. process_args (&argc, argv);
  780. num_c_args = argc + 9;
  781. #ifndef HAVE_LD_DEMANGLE
  782. no_demangle = !! getenv ("COLLECT_NO_DEMANGLE");
  783. /* Suppress demangling by the real linker, which may be broken. */
  784. putenv (xstrdup ("COLLECT_NO_DEMANGLE=1"));
  785. #endif
  786. #if defined (COLLECT2_HOST_INITIALIZATION)
  787. /* Perform system dependent initialization, if necessary. */
  788. COLLECT2_HOST_INITIALIZATION;
  789. #endif
  790. #ifdef SIGCHLD
  791. /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
  792. receive the signal. A different setting is inheritable */
  793. signal (SIGCHLD, SIG_DFL);
  794. #endif
  795. /* Unlock the stdio streams. */
  796. unlock_std_streams ();
  797. gcc_init_libintl ();
  798. diagnostic_initialize (global_dc, 0);
  799. if (atexit (collect_atexit) != 0)
  800. fatal_error (input_location, "atexit failed");
  801. /* Do not invoke xcalloc before this point, since locale needs to be
  802. set first, in case a diagnostic is issued. */
  803. ld1_argv = XCNEWVEC (char *, argc + 4);
  804. ld1 = CONST_CAST2 (const char **, char **, ld1_argv);
  805. ld2_argv = XCNEWVEC (char *, argc + 11);
  806. ld2 = CONST_CAST2 (const char **, char **, ld2_argv);
  807. object_lst = XCNEWVEC (char *, argc);
  808. object = CONST_CAST2 (const char **, char **, object_lst);
  809. #ifdef DEBUG
  810. debug = 1;
  811. #endif
  812. /* Parse command line early for instances of -debug. This allows
  813. the debug flag to be set before functions like find_a_file()
  814. are called. We also look for the -flto or -flto-partition=none flag to know
  815. what LTO mode we are in. */
  816. {
  817. bool no_partition = false;
  818. for (i = 1; argv[i] != NULL; i ++)
  819. {
  820. if (! strcmp (argv[i], "-debug"))
  821. debug = true;
  822. else if (! strcmp (argv[i], "-flto-partition=none"))
  823. no_partition = true;
  824. else if (!strncmp (argv[i], "-fno-lto", 8))
  825. lto_mode = LTO_MODE_NONE;
  826. else if (! strcmp (argv[i], "-plugin"))
  827. {
  828. use_plugin = true;
  829. if (selected_linker == USE_DEFAULT_LD)
  830. selected_linker = USE_PLUGIN_LD;
  831. }
  832. else if (strcmp (argv[i], "-fuse-ld=bfd") == 0)
  833. selected_linker = USE_BFD_LD;
  834. else if (strcmp (argv[i], "-fuse-ld=gold") == 0)
  835. selected_linker = USE_GOLD_LD;
  836. #ifdef COLLECT_EXPORT_LIST
  837. /* These flags are position independent, although their order
  838. is important - subsequent flags override earlier ones. */
  839. else if (strcmp (argv[i], "-b64") == 0)
  840. aix64_flag = 1;
  841. /* -bexport:filename always needs the :filename */
  842. else if (strncmp (argv[i], "-bE:", 4) == 0
  843. || strncmp (argv[i], "-bexport:", 9) == 0)
  844. export_flag = 1;
  845. else if (strcmp (argv[i], "-brtl") == 0
  846. || strcmp (argv[i], "-bsvr4") == 0
  847. || strcmp (argv[i], "-G") == 0)
  848. aixrtl_flag = 1;
  849. else if (strcmp (argv[i], "-bnortl") == 0)
  850. aixrtl_flag = 0;
  851. else if (strcmp (argv[i], "-blazy") == 0)
  852. aixlazy_flag = 1;
  853. #endif
  854. }
  855. verbose = debug;
  856. find_file_set_debug (debug);
  857. if (use_plugin)
  858. lto_mode = LTO_MODE_NONE;
  859. if (no_partition && lto_mode == LTO_MODE_WHOPR)
  860. lto_mode = LTO_MODE_LTO;
  861. }
  862. #ifndef DEFAULT_A_OUT_NAME
  863. output_file = "a.out";
  864. #else
  865. output_file = DEFAULT_A_OUT_NAME;
  866. #endif
  867. obstack_begin (&temporary_obstack, 0);
  868. temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
  869. #ifndef HAVE_LD_DEMANGLE
  870. current_demangling_style = auto_demangling;
  871. #endif
  872. p = getenv ("COLLECT_GCC_OPTIONS");
  873. while (p && *p)
  874. {
  875. const char *q = extract_string (&p);
  876. if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
  877. num_c_args++;
  878. }
  879. obstack_free (&temporary_obstack, temporary_firstobj);
  880. /* -fno-profile-arcs -fno-test-coverage -fno-branch-probabilities
  881. -fno-exceptions -w -fno-whole-program */
  882. num_c_args += 6;
  883. c_argv = XCNEWVEC (char *, num_c_args);
  884. c_ptr = CONST_CAST2 (const char **, char **, c_argv);
  885. if (argc < 2)
  886. fatal_error (input_location, "no arguments");
  887. #ifdef SIGQUIT
  888. if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
  889. signal (SIGQUIT, handler);
  890. #endif
  891. if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  892. signal (SIGINT, handler);
  893. #ifdef SIGALRM
  894. if (signal (SIGALRM, SIG_IGN) != SIG_IGN)
  895. signal (SIGALRM, handler);
  896. #endif
  897. #ifdef SIGHUP
  898. if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  899. signal (SIGHUP, handler);
  900. #endif
  901. if (signal (SIGSEGV, SIG_IGN) != SIG_IGN)
  902. signal (SIGSEGV, handler);
  903. #ifdef SIGBUS
  904. if (signal (SIGBUS, SIG_IGN) != SIG_IGN)
  905. signal (SIGBUS, handler);
  906. #endif
  907. /* Extract COMPILER_PATH and PATH into our prefix list. */
  908. prefix_from_env ("COMPILER_PATH", &cpath);
  909. prefix_from_env ("PATH", &path);
  910. /* Try to discover a valid linker/nm/strip to use. */
  911. /* Maybe we know the right file to use (if not cross). */
  912. ld_file_name = 0;
  913. #ifdef DEFAULT_LINKER
  914. if (selected_linker == USE_BFD_LD || selected_linker == USE_GOLD_LD)
  915. {
  916. char *linker_name;
  917. # ifdef HOST_EXECUTABLE_SUFFIX
  918. int len = (sizeof (DEFAULT_LINKER)
  919. - sizeof (HOST_EXECUTABLE_SUFFIX));
  920. linker_name = NULL;
  921. if (len > 0)
  922. {
  923. char *default_linker = xstrdup (DEFAULT_LINKER);
  924. /* Strip HOST_EXECUTABLE_SUFFIX if DEFAULT_LINKER contains
  925. HOST_EXECUTABLE_SUFFIX. */
  926. if (! strcmp (&default_linker[len], HOST_EXECUTABLE_SUFFIX))
  927. {
  928. default_linker[len] = '\0';
  929. linker_name = concat (default_linker,
  930. &ld_suffixes[selected_linker][2],
  931. HOST_EXECUTABLE_SUFFIX, NULL);
  932. }
  933. }
  934. if (linker_name == NULL)
  935. # endif
  936. linker_name = concat (DEFAULT_LINKER,
  937. &ld_suffixes[selected_linker][2],
  938. NULL);
  939. if (access (linker_name, X_OK) == 0)
  940. ld_file_name = linker_name;
  941. }
  942. if (ld_file_name == 0 && access (DEFAULT_LINKER, X_OK) == 0)
  943. ld_file_name = DEFAULT_LINKER;
  944. if (ld_file_name == 0)
  945. #endif
  946. #ifdef REAL_LD_FILE_NAME
  947. ld_file_name = find_a_file (&path, REAL_LD_FILE_NAME, X_OK);
  948. if (ld_file_name == 0)
  949. #endif
  950. /* Search the (target-specific) compiler dirs for ld'. */
  951. ld_file_name = find_a_file (&cpath, real_ld_suffix, X_OK);
  952. /* Likewise for `collect-ld'. */
  953. if (ld_file_name == 0)
  954. {
  955. ld_file_name = find_a_file (&cpath, collect_ld_suffix, X_OK);
  956. use_collect_ld = ld_file_name != 0;
  957. }
  958. /* Search the compiler directories for `ld'. We have protection against
  959. recursive calls in find_a_file. */
  960. if (ld_file_name == 0)
  961. ld_file_name = find_a_file (&cpath, ld_suffixes[selected_linker], X_OK);
  962. /* Search the ordinary system bin directories
  963. for `ld' (if native linking) or `TARGET-ld' (if cross). */
  964. if (ld_file_name == 0)
  965. ld_file_name = find_a_file (&path, full_ld_suffixes[selected_linker], X_OK);
  966. #ifdef REAL_NM_FILE_NAME
  967. nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME, X_OK);
  968. if (nm_file_name == 0)
  969. #endif
  970. nm_file_name = find_a_file (&cpath, gnm_suffix, X_OK);
  971. if (nm_file_name == 0)
  972. nm_file_name = find_a_file (&path, full_gnm_suffix, X_OK);
  973. if (nm_file_name == 0)
  974. nm_file_name = find_a_file (&cpath, nm_suffix, X_OK);
  975. if (nm_file_name == 0)
  976. nm_file_name = find_a_file (&path, full_nm_suffix, X_OK);
  977. #ifdef LDD_SUFFIX
  978. ldd_file_name = find_a_file (&cpath, ldd_suffix, X_OK);
  979. if (ldd_file_name == 0)
  980. ldd_file_name = find_a_file (&path, full_ldd_suffix, X_OK);
  981. #endif
  982. #ifdef REAL_STRIP_FILE_NAME
  983. strip_file_name = find_a_file (&path, REAL_STRIP_FILE_NAME, X_OK);
  984. if (strip_file_name == 0)
  985. #endif
  986. strip_file_name = find_a_file (&cpath, gstrip_suffix, X_OK);
  987. if (strip_file_name == 0)
  988. strip_file_name = find_a_file (&path, full_gstrip_suffix, X_OK);
  989. if (strip_file_name == 0)
  990. strip_file_name = find_a_file (&cpath, strip_suffix, X_OK);
  991. if (strip_file_name == 0)
  992. strip_file_name = find_a_file (&path, full_strip_suffix, X_OK);
  993. /* Determine the full path name of the C compiler to use. */
  994. c_file_name = getenv ("COLLECT_GCC");
  995. if (c_file_name == 0)
  996. {
  997. #ifdef CROSS_DIRECTORY_STRUCTURE
  998. c_file_name = concat (target_machine, "-gcc", NULL);
  999. #else
  1000. c_file_name = "gcc";
  1001. #endif
  1002. }
  1003. p = find_a_file (&cpath, c_file_name, X_OK);
  1004. /* Here it should be safe to use the system search path since we should have
  1005. already qualified the name of the compiler when it is needed. */
  1006. if (p == 0)
  1007. p = find_a_file (&path, c_file_name, X_OK);
  1008. if (p)
  1009. c_file_name = p;
  1010. *ld1++ = *ld2++ = ld_file_name;
  1011. /* Make temp file names. */
  1012. c_file = make_temp_file (".c");
  1013. o_file = make_temp_file (".o");
  1014. #ifdef COLLECT_EXPORT_LIST
  1015. export_file = make_temp_file (".x");
  1016. #endif
  1017. if (!debug)
  1018. {
  1019. ldout = make_temp_file (".ld");
  1020. lderrout = make_temp_file (".le");
  1021. }
  1022. *c_ptr++ = c_file_name;
  1023. *c_ptr++ = "-x";
  1024. *c_ptr++ = "c";
  1025. *c_ptr++ = "-c";
  1026. *c_ptr++ = "-o";
  1027. *c_ptr++ = o_file;
  1028. #ifdef COLLECT_EXPORT_LIST
  1029. /* Generate a list of directories from LIBPATH. */
  1030. prefix_from_env ("LIBPATH", &libpath_lib_dirs);
  1031. /* Add to this list also two standard directories where
  1032. AIX loader always searches for libraries. */
  1033. add_prefix (&libpath_lib_dirs, "/lib");
  1034. add_prefix (&libpath_lib_dirs, "/usr/lib");
  1035. #endif
  1036. /* Get any options that the upper GCC wants to pass to the sub-GCC.
  1037. AIX support needs to know if -shared has been specified before
  1038. parsing commandline arguments. */
  1039. p = getenv ("COLLECT_GCC_OPTIONS");
  1040. while (p && *p)
  1041. {
  1042. const char *q = extract_string (&p);
  1043. if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
  1044. *c_ptr++ = xstrdup (q);
  1045. if (strcmp (q, "-EL") == 0 || strcmp (q, "-EB") == 0)
  1046. *c_ptr++ = xstrdup (q);
  1047. if (strcmp (q, "-shared") == 0)
  1048. shared_obj = 1;
  1049. if (*q == '-' && q[1] == 'B')
  1050. {
  1051. *c_ptr++ = xstrdup (q);
  1052. if (q[2] == 0)
  1053. {
  1054. q = extract_string (&p);
  1055. *c_ptr++ = xstrdup (q);
  1056. }
  1057. }
  1058. }
  1059. obstack_free (&temporary_obstack, temporary_firstobj);
  1060. *c_ptr++ = "-fno-profile-arcs";
  1061. *c_ptr++ = "-fno-test-coverage";
  1062. *c_ptr++ = "-fno-branch-probabilities";
  1063. *c_ptr++ = "-fno-exceptions";
  1064. *c_ptr++ = "-w";
  1065. *c_ptr++ = "-fno-whole-program";
  1066. /* !!! When GCC calls collect2,
  1067. it does not know whether it is calling collect2 or ld.
  1068. So collect2 cannot meaningfully understand any options
  1069. except those ld understands.
  1070. If you propose to make GCC pass some other option,
  1071. just imagine what will happen if ld is really ld!!! */
  1072. /* Parse arguments. Remember output file spec, pass the rest to ld. */
  1073. /* After the first file, put in the c++ rt0. */
  1074. first_file = 1;
  1075. while ((arg = *++argv) != (char *) 0)
  1076. {
  1077. *ld1++ = *ld2++ = arg;
  1078. if (arg[0] == '-')
  1079. {
  1080. switch (arg[1])
  1081. {
  1082. case 'd':
  1083. if (!strcmp (arg, "-debug"))
  1084. {
  1085. /* Already parsed. */
  1086. ld1--;
  1087. ld2--;
  1088. }
  1089. if (!strcmp (arg, "-dynamic-linker") && argv[1])
  1090. {
  1091. ++argv;
  1092. *ld1++ = *ld2++ = *argv;
  1093. }
  1094. break;
  1095. case 'f':
  1096. if (strncmp (arg, "-flto", 5) == 0)
  1097. {
  1098. #ifdef ENABLE_LTO
  1099. /* Do not pass LTO flag to the linker. */
  1100. ld1--;
  1101. ld2--;
  1102. #else
  1103. error ("LTO support has not been enabled in this "
  1104. "configuration");
  1105. #endif
  1106. }
  1107. else if (!use_collect_ld
  1108. && strncmp (arg, "-fuse-ld=", 9) == 0)
  1109. {
  1110. /* Do not pass -fuse-ld={bfd|gold} to the linker. */
  1111. ld1--;
  1112. ld2--;
  1113. }
  1114. else if (strncmp (arg, "-fno-lto", 8) == 0)
  1115. {
  1116. /* Do not pass -fno-lto to the linker. */
  1117. ld1--;
  1118. ld2--;
  1119. }
  1120. #ifdef TARGET_AIX_VERSION
  1121. else
  1122. {
  1123. /* File containing a list of input files to process. */
  1124. FILE *stream;
  1125. char buf[MAXPATHLEN + 2];
  1126. /* Number of additionnal object files. */
  1127. int add_nbr = 0;
  1128. /* Maximum of additionnal object files before vector
  1129. expansion. */
  1130. int add_max = 0;
  1131. const char *list_filename = arg + 2;
  1132. /* Accept -fFILENAME and -f FILENAME. */
  1133. if (*list_filename == '\0' && argv[1])
  1134. {
  1135. ++argv;
  1136. list_filename = *argv;
  1137. *ld1++ = *ld2++ = *argv;
  1138. }
  1139. stream = fopen (list_filename, "r");
  1140. if (stream == NULL)
  1141. fatal_error (input_location, "can't open %s: %m",
  1142. list_filename);
  1143. while (fgets (buf, sizeof buf, stream) != NULL)
  1144. {
  1145. /* Remove end of line. */
  1146. int len = strlen (buf);
  1147. if (len >= 1 && buf[len - 1] =='\n')
  1148. buf[len - 1] = '\0';
  1149. /* Put on object vector.
  1150. Note: we only expanse vector here, so we must keep
  1151. extra space for remaining arguments. */
  1152. if (add_nbr >= add_max)
  1153. {
  1154. int pos =
  1155. object - CONST_CAST2 (const char **, char **,
  1156. object_lst);
  1157. add_max = (add_max == 0) ? 16 : add_max * 2;
  1158. object_lst = XRESIZEVEC (char *, object_lst,
  1159. object_nbr + add_max);
  1160. object = CONST_CAST2 (const char **, char **,
  1161. object_lst) + pos;
  1162. object_nbr += add_max;
  1163. }
  1164. *object++ = xstrdup (buf);
  1165. add_nbr++;
  1166. }
  1167. fclose (stream);
  1168. }
  1169. #endif
  1170. break;
  1171. case 'l':
  1172. if (first_file)
  1173. {
  1174. /* place o_file BEFORE this argument! */
  1175. first_file = 0;
  1176. ld2--;
  1177. *ld2++ = o_file;
  1178. *ld2++ = arg;
  1179. }
  1180. #ifdef COLLECT_EXPORT_LIST
  1181. {
  1182. /* Resolving full library name. */
  1183. const char *s = resolve_lib_name (arg+2);
  1184. /* Saving a full library name. */
  1185. add_to_list (&libs, s);
  1186. }
  1187. #endif
  1188. break;
  1189. #ifdef COLLECT_EXPORT_LIST
  1190. /* Saving directories where to search for libraries. */
  1191. case 'L':
  1192. add_prefix (&cmdline_lib_dirs, arg+2);
  1193. break;
  1194. #endif
  1195. case 'o':
  1196. if (arg[2] == '\0')
  1197. output_file = *ld1++ = *ld2++ = *++argv;
  1198. else
  1199. output_file = &arg[2];
  1200. break;
  1201. case 'r':
  1202. if (arg[2] == '\0')
  1203. rflag = 1;
  1204. break;
  1205. case 's':
  1206. if (arg[2] == '\0' && do_collecting)
  1207. {
  1208. /* We must strip after the nm run, otherwise C++ linking
  1209. will not work. Thus we strip in the second ld run, or
  1210. else with strip if there is no second ld run. */
  1211. strip_flag = 1;
  1212. ld1--;
  1213. }
  1214. break;
  1215. case 'v':
  1216. if (arg[2] == '\0')
  1217. verbose = true;
  1218. break;
  1219. case '-':
  1220. if (strcmp (arg, "--no-demangle") == 0)
  1221. {
  1222. #ifndef HAVE_LD_DEMANGLE
  1223. no_demangle = 1;
  1224. ld1--;
  1225. ld2--;
  1226. #endif
  1227. }
  1228. else if (strncmp (arg, "--demangle", 10) == 0)
  1229. {
  1230. #ifndef HAVE_LD_DEMANGLE
  1231. no_demangle = 0;
  1232. if (arg[10] == '=')
  1233. {
  1234. enum demangling_styles style
  1235. = cplus_demangle_name_to_style (arg+11);
  1236. if (style == unknown_demangling)
  1237. error ("unknown demangling style '%s'", arg+11);
  1238. else
  1239. current_demangling_style = style;
  1240. }
  1241. ld1--;
  1242. ld2--;
  1243. #endif
  1244. }
  1245. else if (strncmp (arg, "--sysroot=", 10) == 0)
  1246. target_system_root = arg + 10;
  1247. else if (strcmp (arg, "--version") == 0)
  1248. verbose = true;
  1249. else if (strcmp (arg, "--help") == 0)
  1250. helpflag = true;
  1251. break;
  1252. }
  1253. }
  1254. else if ((p = strrchr (arg, '.')) != (char *) 0
  1255. && (strcmp (p, ".o") == 0 || strcmp (p, ".a") == 0
  1256. || strcmp (p, ".so") == 0 || strcmp (p, ".lo") == 0
  1257. || strcmp (p, ".obj") == 0))
  1258. {
  1259. if (first_file)
  1260. {
  1261. first_file = 0;
  1262. if (p[1] == 'o')
  1263. *ld2++ = o_file;
  1264. else
  1265. {
  1266. /* place o_file BEFORE this argument! */
  1267. ld2--;
  1268. *ld2++ = o_file;
  1269. *ld2++ = arg;
  1270. }
  1271. }
  1272. if (p[1] == 'o' || p[1] == 'l')
  1273. *object++ = arg;
  1274. #ifdef COLLECT_EXPORT_LIST
  1275. /* libraries can be specified directly, i.e. without -l flag. */
  1276. else
  1277. {
  1278. /* Saving a full library name. */
  1279. add_to_list (&libs, arg);
  1280. }
  1281. #endif
  1282. }
  1283. }
  1284. #ifdef COLLECT_EXPORT_LIST
  1285. /* This is added only for debugging purposes. */
  1286. if (debug)
  1287. {
  1288. fprintf (stderr, "List of libraries:\n");
  1289. dump_list (stderr, "\t", libs.first);
  1290. }
  1291. /* The AIX linker will discard static constructors in object files if
  1292. nothing else in the file is referenced, so look at them first. Unless
  1293. we are building a shared object, ignore the eh frame tables, as we
  1294. would otherwise reference them all, hence drag all the corresponding
  1295. objects even if nothing else is referenced. */
  1296. {
  1297. const char **export_object_lst
  1298. = CONST_CAST2 (const char **, char **, object_lst);
  1299. struct id *list = libs.first;
  1300. /* Compute the filter to use from the current one, do scan, then adjust
  1301. the "current" filter to remove what we just included here. This will
  1302. control whether we need a first pass link later on or not, and what
  1303. will remain to be scanned there. */
  1304. scanfilter this_filter = ld1_filter;
  1305. #if HAVE_AS_REF
  1306. if (!shared_obj)
  1307. this_filter &= ~SCAN_DWEH;
  1308. #endif
  1309. while (export_object_lst < object)
  1310. scan_prog_file (*export_object_lst++, PASS_OBJ, this_filter);
  1311. for (; list; list = list->next)
  1312. scan_prog_file (list->name, PASS_FIRST, this_filter);
  1313. ld1_filter = ld1_filter & ~this_filter;
  1314. }
  1315. if (exports.first)
  1316. {
  1317. char *buf = concat ("-bE:", export_file, NULL);
  1318. *ld1++ = buf;
  1319. *ld2++ = buf;
  1320. exportf = fopen (export_file, "w");
  1321. if (exportf == (FILE *) 0)
  1322. fatal_error (input_location, "fopen %s: %m", export_file);
  1323. write_aix_file (exportf, exports.first);
  1324. if (fclose (exportf))
  1325. fatal_error (input_location, "fclose %s: %m", export_file);
  1326. }
  1327. #endif
  1328. *c_ptr++ = c_file;
  1329. *c_ptr = *ld1 = *object = (char *) 0;
  1330. if (verbose)
  1331. notice ("collect2 version %s\n", version_string);
  1332. if (helpflag)
  1333. {
  1334. printf ("Usage: collect2 [options]\n");
  1335. printf (" Wrap linker and generate constructor code if needed.\n");
  1336. printf (" Options:\n");
  1337. printf (" -debug Enable debug output\n");
  1338. printf (" --help Display this information\n");
  1339. printf (" -v, --version Display this program's version number\n");
  1340. printf ("\n");
  1341. printf ("Overview: http://gcc.gnu.org/onlinedocs/gccint/Collect2.html\n");
  1342. printf ("Report bugs: %s\n", bug_report_url);
  1343. printf ("\n");
  1344. }
  1345. if (debug)
  1346. {
  1347. const char *ptr;
  1348. fprintf (stderr, "ld_file_name = %s\n",
  1349. (ld_file_name ? ld_file_name : "not found"));
  1350. fprintf (stderr, "c_file_name = %s\n",
  1351. (c_file_name ? c_file_name : "not found"));
  1352. fprintf (stderr, "nm_file_name = %s\n",
  1353. (nm_file_name ? nm_file_name : "not found"));
  1354. #ifdef LDD_SUFFIX
  1355. fprintf (stderr, "ldd_file_name = %s\n",
  1356. (ldd_file_name ? ldd_file_name : "not found"));
  1357. #endif
  1358. fprintf (stderr, "strip_file_name = %s\n",
  1359. (strip_file_name ? strip_file_name : "not found"));
  1360. fprintf (stderr, "c_file = %s\n",
  1361. (c_file ? c_file : "not found"));
  1362. fprintf (stderr, "o_file = %s\n",
  1363. (o_file ? o_file : "not found"));
  1364. ptr = getenv ("COLLECT_GCC_OPTIONS");
  1365. if (ptr)
  1366. fprintf (stderr, "COLLECT_GCC_OPTIONS = %s\n", ptr);
  1367. ptr = getenv ("COLLECT_GCC");
  1368. if (ptr)
  1369. fprintf (stderr, "COLLECT_GCC = %s\n", ptr);
  1370. ptr = getenv ("COMPILER_PATH");
  1371. if (ptr)
  1372. fprintf (stderr, "COMPILER_PATH = %s\n", ptr);
  1373. ptr = getenv (LIBRARY_PATH_ENV);
  1374. if (ptr)
  1375. fprintf (stderr, "%-20s= %s\n", LIBRARY_PATH_ENV, ptr);
  1376. fprintf (stderr, "\n");
  1377. }
  1378. /* Load the program, searching all libraries and attempting to provide
  1379. undefined symbols from repository information.
  1380. If -r or they will be run via some other method, do not build the
  1381. constructor or destructor list, just return now. */
  1382. {
  1383. bool early_exit
  1384. = rflag || (! DO_COLLECT_EXPORT_LIST && ! do_collecting);
  1385. /* Perform the first pass link now, if we're about to exit or if we need
  1386. to scan for things we haven't collected yet before pursuing further.
  1387. On AIX, the latter typically includes nothing for shared objects or
  1388. frame tables for an executable, out of what the required early scan on
  1389. objects and libraries has performed above. In the !shared_obj case, we
  1390. expect the relevant tables to be dragged together with their associated
  1391. functions from precise cross reference insertions by the compiler. */
  1392. if (early_exit || ld1_filter != SCAN_NOTHING)
  1393. do_tlink (ld1_argv, object_lst);
  1394. if (early_exit)
  1395. {
  1396. #ifdef COLLECT_EXPORT_LIST
  1397. /* Make sure we delete the export file we may have created. */
  1398. if (export_file != 0 && export_file[0])
  1399. maybe_unlink (export_file);
  1400. #endif
  1401. if (lto_mode != LTO_MODE_NONE)
  1402. maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
  1403. else
  1404. post_ld_pass (false);
  1405. maybe_unlink (c_file);
  1406. maybe_unlink (o_file);
  1407. return 0;
  1408. }
  1409. }
  1410. /* Unless we have done it all already, examine the namelist and search for
  1411. static constructors and destructors to call. Write the constructor and
  1412. destructor tables to a .s file and reload. */
  1413. if (ld1_filter != SCAN_NOTHING)
  1414. scan_prog_file (output_file, PASS_FIRST, ld1_filter);
  1415. #ifdef SCAN_LIBRARIES
  1416. scan_libraries (output_file);
  1417. #endif
  1418. if (debug)
  1419. {
  1420. notice_translated (ngettext ("%d constructor found\n",
  1421. "%d constructors found\n",
  1422. constructors.number),
  1423. constructors.number);
  1424. notice_translated (ngettext ("%d destructor found\n",
  1425. "%d destructors found\n",
  1426. destructors.number),
  1427. destructors.number);
  1428. notice_translated (ngettext ("%d frame table found\n",
  1429. "%d frame tables found\n",
  1430. frame_tables.number),
  1431. frame_tables.number);
  1432. }
  1433. /* If the scan exposed nothing of special interest, there's no need to
  1434. generate the glue code and relink so return now. */
  1435. if (constructors.number == 0 && destructors.number == 0
  1436. && frame_tables.number == 0
  1437. #if defined (SCAN_LIBRARIES) || defined (COLLECT_EXPORT_LIST)
  1438. /* If we will be running these functions ourselves, we want to emit
  1439. stubs into the shared library so that we do not have to relink
  1440. dependent programs when we add static objects. */
  1441. && ! shared_obj
  1442. #endif
  1443. )
  1444. {
  1445. /* Do tlink without additional code generation now if we didn't
  1446. do it earlier for scanning purposes. */
  1447. if (ld1_filter == SCAN_NOTHING)
  1448. do_tlink (ld1_argv, object_lst);
  1449. if (lto_mode)
  1450. maybe_run_lto_and_relink (ld1_argv, object_lst, object, false);
  1451. /* Strip now if it was requested on the command line. */
  1452. if (strip_flag)
  1453. {
  1454. char **real_strip_argv = XCNEWVEC (char *, 3);
  1455. const char ** strip_argv = CONST_CAST2 (const char **, char **,
  1456. real_strip_argv);
  1457. strip_argv[0] = strip_file_name;
  1458. strip_argv[1] = output_file;
  1459. strip_argv[2] = (char *) 0;
  1460. fork_execute ("strip", real_strip_argv, false);
  1461. }
  1462. #ifdef COLLECT_EXPORT_LIST
  1463. maybe_unlink (export_file);
  1464. #endif
  1465. post_ld_pass (false);
  1466. maybe_unlink (c_file);
  1467. maybe_unlink (o_file);
  1468. return 0;
  1469. }
  1470. /* Sort ctor and dtor lists by priority. */
  1471. sort_ids (&constructors);
  1472. sort_ids (&destructors);
  1473. maybe_unlink (output_file);
  1474. outf = fopen (c_file, "w");
  1475. if (outf == (FILE *) 0)
  1476. fatal_error (input_location, "fopen %s: %m", c_file);
  1477. write_c_file (outf, c_file);
  1478. if (fclose (outf))
  1479. fatal_error (input_location, "fclose %s: %m", c_file);
  1480. /* Tell the linker that we have initializer and finalizer functions. */
  1481. #ifdef LD_INIT_SWITCH
  1482. #ifdef COLLECT_EXPORT_LIST
  1483. *ld2++ = concat (LD_INIT_SWITCH, ":", initname, ":", fininame, NULL);
  1484. #else
  1485. *ld2++ = LD_INIT_SWITCH;
  1486. *ld2++ = initname;
  1487. *ld2++ = LD_FINI_SWITCH;
  1488. *ld2++ = fininame;
  1489. #endif
  1490. #endif
  1491. #ifdef COLLECT_EXPORT_LIST
  1492. if (shared_obj)
  1493. {
  1494. /* If we did not add export flag to link arguments before, add it to
  1495. second link phase now. No new exports should have been added. */
  1496. if (! exports.first)
  1497. *ld2++ = concat ("-bE:", export_file, NULL);
  1498. #ifdef TARGET_AIX_VERSION
  1499. add_to_list (&exports, aix_shared_initname);
  1500. add_to_list (&exports, aix_shared_fininame);
  1501. #endif
  1502. #ifndef LD_INIT_SWITCH
  1503. add_to_list (&exports, initname);
  1504. add_to_list (&exports, fininame);
  1505. add_to_list (&exports, "_GLOBAL__DI");
  1506. add_to_list (&exports, "_GLOBAL__DD");
  1507. #endif
  1508. exportf = fopen (export_file, "w");
  1509. if (exportf == (FILE *) 0)
  1510. fatal_error (input_location, "fopen %s: %m", export_file);
  1511. write_aix_file (exportf, exports.first);
  1512. if (fclose (exportf))
  1513. fatal_error (input_location, "fclose %s: %m", export_file);
  1514. }
  1515. #endif
  1516. /* End of arguments to second link phase. */
  1517. *ld2 = (char*) 0;
  1518. if (debug)
  1519. {
  1520. fprintf (stderr, "\n========== output_file = %s, c_file = %s\n",
  1521. output_file, c_file);
  1522. write_c_file (stderr, "stderr");
  1523. fprintf (stderr, "========== end of c_file\n\n");
  1524. #ifdef COLLECT_EXPORT_LIST
  1525. fprintf (stderr, "\n========== export_file = %s\n", export_file);
  1526. write_aix_file (stderr, exports.first);
  1527. fprintf (stderr, "========== end of export_file\n\n");
  1528. #endif
  1529. }
  1530. /* Assemble the constructor and destructor tables.
  1531. Link the tables in with the rest of the program. */
  1532. fork_execute ("gcc", c_argv, at_file_supplied);
  1533. #ifdef COLLECT_EXPORT_LIST
  1534. /* On AIX we must call tlink because of possible templates resolution. */
  1535. do_tlink (ld2_argv, object_lst);
  1536. if (lto_mode)
  1537. maybe_run_lto_and_relink (ld2_argv, object_lst, object, false);
  1538. #else
  1539. /* Otherwise, simply call ld because tlink is already done. */
  1540. if (lto_mode)
  1541. maybe_run_lto_and_relink (ld2_argv, object_lst, object, true);
  1542. else
  1543. {
  1544. fork_execute ("ld", ld2_argv, HAVE_GNU_LD && at_file_supplied);
  1545. post_ld_pass (false);
  1546. }
  1547. /* Let scan_prog_file do any final mods (OSF/rose needs this for
  1548. constructors/destructors in shared libraries. */
  1549. scan_prog_file (output_file, PASS_SECOND, SCAN_ALL);
  1550. #endif
  1551. maybe_unlink (c_file);
  1552. maybe_unlink (o_file);
  1553. #ifdef COLLECT_EXPORT_LIST
  1554. maybe_unlink (export_file);
  1555. #endif
  1556. return 0;
  1557. }
  1558. /* Unlink FILE unless we are debugging or this is the output_file
  1559. and we may not unlink it. */
  1560. void
  1561. maybe_unlink (const char *file)
  1562. {
  1563. if (debug)
  1564. {
  1565. notice ("[Leaving %s]\n", file);
  1566. return;
  1567. }
  1568. if (file == output_file && !may_unlink_output_file)
  1569. return;
  1570. unlink_if_ordinary (file);
  1571. }
  1572. /* Call maybe_unlink on the NULL-terminated list, FILE_LIST. */
  1573. static void
  1574. maybe_unlink_list (char **file_list)
  1575. {
  1576. char **tmp = file_list;
  1577. while (*tmp)
  1578. maybe_unlink (*(tmp++));
  1579. }
  1580. static long sequence_number = 0;
  1581. /* Add a name to a linked list. */
  1582. static void
  1583. add_to_list (struct head *head_ptr, const char *name)
  1584. {
  1585. struct id *newid
  1586. = (struct id *) xcalloc (sizeof (struct id) + strlen (name), 1);
  1587. struct id *p;
  1588. strcpy (newid->name, name);
  1589. if (head_ptr->first)
  1590. head_ptr->last->next = newid;
  1591. else
  1592. head_ptr->first = newid;
  1593. /* Check for duplicate symbols. */
  1594. for (p = head_ptr->first;
  1595. strcmp (name, p->name) != 0;
  1596. p = p->next)
  1597. ;
  1598. if (p != newid)
  1599. {
  1600. head_ptr->last->next = 0;
  1601. free (newid);
  1602. return;
  1603. }
  1604. newid->sequence = ++sequence_number;
  1605. head_ptr->last = newid;
  1606. head_ptr->number++;
  1607. }
  1608. /* Grab the init priority number from an init function name that
  1609. looks like "_GLOBAL_.I.12345.foo". */
  1610. static int
  1611. extract_init_priority (const char *name)
  1612. {
  1613. int pos = 0, pri;
  1614. #ifdef TARGET_AIX_VERSION
  1615. /* Run dependent module initializers before any constructors in this
  1616. module. */
  1617. switch (is_ctor_dtor (name))
  1618. {
  1619. case SYM_AIXI:
  1620. case SYM_AIXD:
  1621. return INT_MIN;
  1622. default:
  1623. break;
  1624. }
  1625. #endif
  1626. while (name[pos] == '_')
  1627. ++pos;
  1628. pos += 10; /* strlen ("GLOBAL__X_") */
  1629. /* Extract init_p number from ctor/dtor name. */
  1630. pri = atoi (name + pos);
  1631. return pri ? pri : DEFAULT_INIT_PRIORITY;
  1632. }
  1633. /* Insertion sort the ids from ctor/dtor list HEAD_PTR in descending order.
  1634. ctors will be run from right to left, dtors from left to right. */
  1635. static void
  1636. sort_ids (struct head *head_ptr)
  1637. {
  1638. /* id holds the current element to insert. id_next holds the next
  1639. element to insert. id_ptr iterates through the already sorted elements
  1640. looking for the place to insert id. */
  1641. struct id *id, *id_next, **id_ptr;
  1642. id = head_ptr->first;
  1643. /* We don't have any sorted elements yet. */
  1644. head_ptr->first = NULL;
  1645. for (; id; id = id_next)
  1646. {
  1647. id_next = id->next;
  1648. id->sequence = extract_init_priority (id->name);
  1649. for (id_ptr = &(head_ptr->first); ; id_ptr = &((*id_ptr)->next))
  1650. if (*id_ptr == NULL
  1651. /* If the sequence numbers are the same, we put the id from the
  1652. file later on the command line later in the list. */
  1653. || id->sequence > (*id_ptr)->sequence
  1654. /* Hack: do lexical compare, too.
  1655. || (id->sequence == (*id_ptr)->sequence
  1656. && strcmp (id->name, (*id_ptr)->name) > 0) */
  1657. )
  1658. {
  1659. id->next = *id_ptr;
  1660. *id_ptr = id;
  1661. break;
  1662. }
  1663. }
  1664. /* Now set the sequence numbers properly so write_c_file works. */
  1665. for (id = head_ptr->first; id; id = id->next)
  1666. id->sequence = ++sequence_number;
  1667. }
  1668. /* Write: `prefix', the names on list LIST, `suffix'. */
  1669. static void
  1670. write_list (FILE *stream, const char *prefix, struct id *list)
  1671. {
  1672. while (list)
  1673. {
  1674. fprintf (stream, "%sx%d,\n", prefix, list->sequence);
  1675. list = list->next;
  1676. }
  1677. }
  1678. #ifdef COLLECT_EXPORT_LIST
  1679. /* This function is really used only on AIX, but may be useful. */
  1680. #if 0
  1681. static int
  1682. is_in_list (const char *prefix, struct id *list)
  1683. {
  1684. while (list)
  1685. {
  1686. if (!strcmp (prefix, list->name)) return 1;
  1687. list = list->next;
  1688. }
  1689. return 0;
  1690. }
  1691. #endif
  1692. #endif /* COLLECT_EXPORT_LIST */
  1693. /* Added for debugging purpose. */
  1694. #ifdef COLLECT_EXPORT_LIST
  1695. static void
  1696. dump_list (FILE *stream, const char *prefix, struct id *list)
  1697. {
  1698. while (list)
  1699. {
  1700. fprintf (stream, "%s%s,\n", prefix, list->name);
  1701. list = list->next;
  1702. }
  1703. }
  1704. #endif
  1705. #if 0
  1706. static void
  1707. dump_prefix_list (FILE *stream, const char *prefix, struct prefix_list *list)
  1708. {
  1709. while (list)
  1710. {
  1711. fprintf (stream, "%s%s,\n", prefix, list->prefix);
  1712. list = list->next;
  1713. }
  1714. }
  1715. #endif
  1716. static void
  1717. write_list_with_asm (FILE *stream, const char *prefix, struct id *list)
  1718. {
  1719. while (list)
  1720. {
  1721. fprintf (stream, "%sx%d __asm__ (\"%s\");\n",
  1722. prefix, list->sequence, list->name);
  1723. list = list->next;
  1724. }
  1725. }
  1726. /* Write out the constructor and destructor tables statically (for a shared
  1727. object), along with the functions to execute them. */
  1728. static void
  1729. write_c_file_stat (FILE *stream, const char *name ATTRIBUTE_UNUSED)
  1730. {
  1731. const char *p, *q;
  1732. char *prefix, *r;
  1733. int frames = (frame_tables.number > 0);
  1734. /* Figure out name of output_file, stripping off .so version. */
  1735. q = p = lbasename (output_file);
  1736. while (q)
  1737. {
  1738. q = strchr (q,'.');
  1739. if (q == 0)
  1740. {
  1741. q = p + strlen (p);
  1742. break;
  1743. }
  1744. else
  1745. {
  1746. if (filename_ncmp (q, SHLIB_SUFFIX, strlen (SHLIB_SUFFIX)) == 0)
  1747. {
  1748. q += strlen (SHLIB_SUFFIX);
  1749. break;
  1750. }
  1751. else
  1752. q++;
  1753. }
  1754. }
  1755. /* q points to null at end of the string (or . of the .so version) */
  1756. prefix = XNEWVEC (char, q - p + 1);
  1757. strncpy (prefix, p, q - p);
  1758. prefix[q - p] = 0;
  1759. for (r = prefix; *r; r++)
  1760. if (!ISALNUM ((unsigned char)*r))
  1761. *r = '_';
  1762. if (debug)
  1763. notice ("\nwrite_c_file - output name is %s, prefix is %s\n",
  1764. output_file, prefix);
  1765. initname = concat ("_GLOBAL__FI_", prefix, NULL);
  1766. fininame = concat ("_GLOBAL__FD_", prefix, NULL);
  1767. #ifdef TARGET_AIX_VERSION
  1768. aix_shared_initname = concat ("_GLOBAL__AIXI_", prefix, NULL);
  1769. aix_shared_fininame = concat ("_GLOBAL__AIXD_", prefix, NULL);
  1770. #endif
  1771. free (prefix);
  1772. /* Write the tables as C code. */
  1773. /* This count variable is used to prevent multiple calls to the
  1774. constructors/destructors.
  1775. This guard against multiple calls is important on AIX as the initfini
  1776. functions are deliberately invoked multiple times as part of the
  1777. mechanisms GCC uses to order constructors across different dependent
  1778. shared libraries (see config/rs6000/aix.h).
  1779. */
  1780. fprintf (stream, "static int count;\n");
  1781. fprintf (stream, "typedef void entry_pt();\n");
  1782. write_list_with_asm (stream, "extern entry_pt ", constructors.first);
  1783. if (frames)
  1784. {
  1785. write_list_with_asm (stream, "extern void *", frame_tables.first);
  1786. fprintf (stream, "\tstatic void *frame_table[] = {\n");
  1787. write_list (stream, "\t\t&", frame_tables.first);
  1788. fprintf (stream, "\t0\n};\n");
  1789. /* This must match what's in frame.h. */
  1790. fprintf (stream, "struct object {\n");
  1791. fprintf (stream, " void *pc_begin;\n");
  1792. fprintf (stream, " void *pc_end;\n");
  1793. fprintf (stream, " void *fde_begin;\n");
  1794. fprintf (stream, " void *fde_array;\n");
  1795. fprintf (stream, " __SIZE_TYPE__ count;\n");
  1796. fprintf (stream, " struct object *next;\n");
  1797. fprintf (stream, "};\n");
  1798. fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
  1799. fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
  1800. fprintf (stream, "static void reg_frame () {\n");
  1801. fprintf (stream, "\tstatic struct object ob;\n");
  1802. fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
  1803. fprintf (stream, "\t}\n");
  1804. fprintf (stream, "static void dereg_frame () {\n");
  1805. fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
  1806. fprintf (stream, "\t}\n");
  1807. }
  1808. fprintf (stream, "void %s() {\n", initname);
  1809. if (constructors.number > 0 || frames)
  1810. {
  1811. fprintf (stream, "\tstatic entry_pt *ctors[] = {\n");
  1812. write_list (stream, "\t\t", constructors.first);
  1813. if (frames)
  1814. fprintf (stream, "\treg_frame,\n");
  1815. fprintf (stream, "\t};\n");
  1816. fprintf (stream, "\tentry_pt **p;\n");
  1817. fprintf (stream, "\tif (count++ != 0) return;\n");
  1818. fprintf (stream, "\tp = ctors + %d;\n", constructors.number + frames);
  1819. fprintf (stream, "\twhile (p > ctors) (*--p)();\n");
  1820. }
  1821. else
  1822. fprintf (stream, "\t++count;\n");
  1823. fprintf (stream, "}\n");
  1824. write_list_with_asm (stream, "extern entry_pt ", destructors.first);
  1825. fprintf (stream, "void %s() {\n", fininame);
  1826. if (destructors.number > 0 || frames)
  1827. {
  1828. fprintf (stream, "\tstatic entry_pt *dtors[] = {\n");
  1829. write_list (stream, "\t\t", destructors.first);
  1830. if (frames)
  1831. fprintf (stream, "\tdereg_frame,\n");
  1832. fprintf (stream, "\t};\n");
  1833. fprintf (stream, "\tentry_pt **p;\n");
  1834. fprintf (stream, "\tif (--count != 0) return;\n");
  1835. fprintf (stream, "\tp = dtors;\n");
  1836. fprintf (stream, "\twhile (p < dtors + %d) (*p++)();\n",
  1837. destructors.number + frames);
  1838. }
  1839. fprintf (stream, "}\n");
  1840. if (shared_obj)
  1841. {
  1842. COLLECT_SHARED_INIT_FUNC (stream, initname);
  1843. COLLECT_SHARED_FINI_FUNC (stream, fininame);
  1844. }
  1845. }
  1846. /* Write the constructor/destructor tables. */
  1847. #ifndef LD_INIT_SWITCH
  1848. static void
  1849. write_c_file_glob (FILE *stream, const char *name ATTRIBUTE_UNUSED)
  1850. {
  1851. /* Write the tables as C code. */
  1852. int frames = (frame_tables.number > 0);
  1853. fprintf (stream, "typedef void entry_pt();\n\n");
  1854. write_list_with_asm (stream, "extern entry_pt ", constructors.first);
  1855. if (frames)
  1856. {
  1857. write_list_with_asm (stream, "extern void *", frame_tables.first);
  1858. fprintf (stream, "\tstatic void *frame_table[] = {\n");
  1859. write_list (stream, "\t\t&", frame_tables.first);
  1860. fprintf (stream, "\t0\n};\n");
  1861. /* This must match what's in frame.h. */
  1862. fprintf (stream, "struct object {\n");
  1863. fprintf (stream, " void *pc_begin;\n");
  1864. fprintf (stream, " void *pc_end;\n");
  1865. fprintf (stream, " void *fde_begin;\n");
  1866. fprintf (stream, " void *fde_array;\n");
  1867. fprintf (stream, " __SIZE_TYPE__ count;\n");
  1868. fprintf (stream, " struct object *next;\n");
  1869. fprintf (stream, "};\n");
  1870. fprintf (stream, "extern void __register_frame_info_table (void *, struct object *);\n");
  1871. fprintf (stream, "extern void *__deregister_frame_info (void *);\n");
  1872. fprintf (stream, "static void reg_frame () {\n");
  1873. fprintf (stream, "\tstatic struct object ob;\n");
  1874. fprintf (stream, "\t__register_frame_info_table (frame_table, &ob);\n");
  1875. fprintf (stream, "\t}\n");
  1876. fprintf (stream, "static void dereg_frame () {\n");
  1877. fprintf (stream, "\t__deregister_frame_info (frame_table);\n");
  1878. fprintf (stream, "\t}\n");
  1879. }
  1880. fprintf (stream, "\nentry_pt * __CTOR_LIST__[] = {\n");
  1881. fprintf (stream, "\t(entry_pt *) %d,\n", constructors.number + frames);
  1882. write_list (stream, "\t", constructors.first);
  1883. if (frames)
  1884. fprintf (stream, "\treg_frame,\n");
  1885. fprintf (stream, "\t0\n};\n\n");
  1886. write_list_with_asm (stream, "extern entry_pt ", destructors.first);
  1887. fprintf (stream, "\nentry_pt * __DTOR_LIST__[] = {\n");
  1888. fprintf (stream, "\t(entry_pt *) %d,\n", destructors.number + frames);
  1889. write_list (stream, "\t", destructors.first);
  1890. if (frames)
  1891. fprintf (stream, "\tdereg_frame,\n");
  1892. fprintf (stream, "\t0\n};\n\n");
  1893. fprintf (stream, "extern entry_pt %s;\n", NAME__MAIN);
  1894. fprintf (stream, "entry_pt *__main_reference = %s;\n\n", NAME__MAIN);
  1895. }
  1896. #endif /* ! LD_INIT_SWITCH */
  1897. static void
  1898. write_c_file (FILE *stream, const char *name)
  1899. {
  1900. #ifndef LD_INIT_SWITCH
  1901. if (! shared_obj)
  1902. write_c_file_glob (stream, name);
  1903. else
  1904. #endif
  1905. write_c_file_stat (stream, name);
  1906. }
  1907. #ifdef COLLECT_EXPORT_LIST
  1908. static void
  1909. write_aix_file (FILE *stream, struct id *list)
  1910. {
  1911. for (; list; list = list->next)
  1912. {
  1913. fputs (list->name, stream);
  1914. putc ('\n', stream);
  1915. }
  1916. }
  1917. #endif
  1918. #ifdef OBJECT_FORMAT_NONE
  1919. /* Check to make sure the file is an LTO object file. */
  1920. static bool
  1921. maybe_lto_object_file (const char *prog_name)
  1922. {
  1923. FILE *f;
  1924. unsigned char buf[4];
  1925. int i;
  1926. static unsigned char elfmagic[4] = { 0x7f, 'E', 'L', 'F' };
  1927. static unsigned char coffmagic[2] = { 0x4c, 0x01 };
  1928. static unsigned char coffmagic_x64[2] = { 0x64, 0x86 };
  1929. static unsigned char machomagic[4][4] = {
  1930. { 0xcf, 0xfa, 0xed, 0xfe },
  1931. { 0xce, 0xfa, 0xed, 0xfe },
  1932. { 0xfe, 0xed, 0xfa, 0xcf },
  1933. { 0xfe, 0xed, 0xfa, 0xce }
  1934. };
  1935. f = fopen (prog_name, "rb");
  1936. if (f == NULL)
  1937. return false;
  1938. if (fread (buf, sizeof (buf), 1, f) != 1)
  1939. buf[0] = 0;
  1940. fclose (f);
  1941. if (memcmp (buf, elfmagic, sizeof (elfmagic)) == 0
  1942. || memcmp (buf, coffmagic, sizeof (coffmagic)) == 0
  1943. || memcmp (buf, coffmagic_x64, sizeof (coffmagic_x64)) == 0)
  1944. return true;
  1945. for (i = 0; i < 4; i++)
  1946. if (memcmp (buf, machomagic[i], sizeof (machomagic[i])) == 0)
  1947. return true;
  1948. return false;
  1949. }
  1950. /* Generic version to scan the name list of the loaded program for
  1951. the symbols g++ uses for static constructors and destructors. */
  1952. static void
  1953. scan_prog_file (const char *prog_name, scanpass which_pass,
  1954. scanfilter filter)
  1955. {
  1956. void (*int_handler) (int);
  1957. #ifdef SIGQUIT
  1958. void (*quit_handler) (int);
  1959. #endif
  1960. char *real_nm_argv[4];
  1961. const char **nm_argv = CONST_CAST2 (const char **, char**, real_nm_argv);
  1962. int argc = 0;
  1963. struct pex_obj *pex;
  1964. const char *errmsg;
  1965. int err;
  1966. char *p, buf[1024];
  1967. FILE *inf;
  1968. int found_lto = 0;
  1969. if (which_pass == PASS_SECOND)
  1970. return;
  1971. /* LTO objects must be in a known format. This check prevents
  1972. us from accepting an archive containing LTO objects, which
  1973. gcc cannot currently handle. */
  1974. if (which_pass == PASS_LTOINFO && !maybe_lto_object_file (prog_name))
  1975. return;
  1976. /* If we do not have an `nm', complain. */
  1977. if (nm_file_name == 0)
  1978. fatal_error (input_location, "cannot find 'nm'");
  1979. nm_argv[argc++] = nm_file_name;
  1980. if (NM_FLAGS[0] != '\0')
  1981. nm_argv[argc++] = NM_FLAGS;
  1982. nm_argv[argc++] = prog_name;
  1983. nm_argv[argc++] = (char *) 0;
  1984. /* Trace if needed. */
  1985. if (verbose)
  1986. {
  1987. const char **p_argv;
  1988. const char *str;
  1989. for (p_argv = &nm_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
  1990. fprintf (stderr, " %s", str);
  1991. fprintf (stderr, "\n");
  1992. }
  1993. fflush (stdout);
  1994. fflush (stderr);
  1995. pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
  1996. if (pex == NULL)
  1997. fatal_error (input_location, "pex_init failed: %m");
  1998. errmsg = pex_run (pex, 0, nm_file_name, real_nm_argv, NULL, HOST_BIT_BUCKET,
  1999. &err);
  2000. if (errmsg != NULL)
  2001. {
  2002. if (err != 0)
  2003. {
  2004. errno = err;
  2005. fatal_error (input_location, "%s: %m", _(errmsg));
  2006. }
  2007. else
  2008. fatal_error (input_location, errmsg);
  2009. }
  2010. int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
  2011. #ifdef SIGQUIT
  2012. quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
  2013. #endif
  2014. inf = pex_read_output (pex, 0);
  2015. if (inf == NULL)
  2016. fatal_error (input_location, "can't open nm output: %m");
  2017. if (debug)
  2018. {
  2019. if (which_pass == PASS_LTOINFO)
  2020. fprintf (stderr, "\nnm output with LTO info marker symbol.\n");
  2021. else
  2022. fprintf (stderr, "\nnm output with constructors/destructors.\n");
  2023. }
  2024. /* Read each line of nm output. */
  2025. while (fgets (buf, sizeof buf, inf) != (char *) 0)
  2026. {
  2027. int ch, ch2;
  2028. char *name, *end;
  2029. if (debug)
  2030. fprintf (stderr, "\t%s\n", buf);
  2031. if (which_pass == PASS_LTOINFO)
  2032. {
  2033. if (found_lto)
  2034. continue;
  2035. /* Look for the LTO info marker symbol, and add filename to
  2036. the LTO objects list if found. */
  2037. for (p = buf; (ch = *p) != '\0' && ch != '\n'; p++)
  2038. if (ch == ' ' && p[1] == '_' && p[2] == '_'
  2039. && (strncmp (p + (p[3] == '_' ? 2 : 1), "__gnu_lto_v1", 12) == 0)
  2040. && ISSPACE (p[p[3] == '_' ? 14 : 13]))
  2041. {
  2042. add_lto_object (&lto_objects, prog_name);
  2043. /* We need to read all the input, so we can't just
  2044. return here. But we can avoid useless work. */
  2045. found_lto = 1;
  2046. break;
  2047. }
  2048. continue;
  2049. }
  2050. /* If it contains a constructor or destructor name, add the name
  2051. to the appropriate list unless this is a kind of symbol we're
  2052. not supposed to even consider. */
  2053. for (p = buf; (ch = *p) != '\0' && ch != '\n' && ch != '_'; p++)
  2054. if (ch == ' ' && p[1] == 'U' && p[2] == ' ')
  2055. break;
  2056. if (ch != '_')
  2057. continue;
  2058. name = p;
  2059. /* Find the end of the symbol name.
  2060. Do not include `|', because Encore nm can tack that on the end. */
  2061. for (end = p; (ch2 = *end) != '\0' && !ISSPACE (ch2) && ch2 != '|';
  2062. end++)
  2063. continue;
  2064. *end = '\0';
  2065. switch (is_ctor_dtor (name))
  2066. {
  2067. case SYM_CTOR:
  2068. if (! (filter & SCAN_CTOR))
  2069. break;
  2070. if (which_pass != PASS_LIB)
  2071. add_to_list (&constructors, name);
  2072. break;
  2073. case SYM_DTOR:
  2074. if (! (filter & SCAN_DTOR))
  2075. break;
  2076. if (which_pass != PASS_LIB)
  2077. add_to_list (&destructors, name);
  2078. break;
  2079. case SYM_INIT:
  2080. if (! (filter & SCAN_INIT))
  2081. break;
  2082. if (which_pass != PASS_LIB)
  2083. fatal_error (input_location, "init function found in object %s",
  2084. prog_name);
  2085. #ifndef LD_INIT_SWITCH
  2086. add_to_list (&constructors, name);
  2087. #endif
  2088. break;
  2089. case SYM_FINI:
  2090. if (! (filter & SCAN_FINI))
  2091. break;
  2092. if (which_pass != PASS_LIB)
  2093. fatal_error (input_location, "fini function found in object %s",
  2094. prog_name);
  2095. #ifndef LD_FINI_SWITCH
  2096. add_to_list (&destructors, name);
  2097. #endif
  2098. break;
  2099. case SYM_DWEH:
  2100. if (! (filter & SCAN_DWEH))
  2101. break;
  2102. if (which_pass != PASS_LIB)
  2103. add_to_list (&frame_tables, name);
  2104. break;
  2105. default: /* not a constructor or destructor */
  2106. continue;
  2107. }
  2108. }
  2109. if (debug)
  2110. fprintf (stderr, "\n");
  2111. do_wait (nm_file_name, pex);
  2112. signal (SIGINT, int_handler);
  2113. #ifdef SIGQUIT
  2114. signal (SIGQUIT, quit_handler);
  2115. #endif
  2116. }
  2117. #ifdef LDD_SUFFIX
  2118. /* Use the List Dynamic Dependencies program to find shared libraries that
  2119. the output file depends upon and their initialization/finalization
  2120. routines, if any. */
  2121. static void
  2122. scan_libraries (const char *prog_name)
  2123. {
  2124. static struct head libraries; /* list of shared libraries found */
  2125. struct id *list;
  2126. void (*int_handler) (int);
  2127. #ifdef SIGQUIT
  2128. void (*quit_handler) (int);
  2129. #endif
  2130. char *real_ldd_argv[4];
  2131. const char **ldd_argv = CONST_CAST2 (const char **, char **, real_ldd_argv);
  2132. int argc = 0;
  2133. struct pex_obj *pex;
  2134. const char *errmsg;
  2135. int err;
  2136. char buf[1024];
  2137. FILE *inf;
  2138. /* If we do not have an `ldd', complain. */
  2139. if (ldd_file_name == 0)
  2140. {
  2141. error ("cannot find 'ldd'");
  2142. return;
  2143. }
  2144. ldd_argv[argc++] = ldd_file_name;
  2145. ldd_argv[argc++] = prog_name;
  2146. ldd_argv[argc++] = (char *) 0;
  2147. /* Trace if needed. */
  2148. if (verbose)
  2149. {
  2150. const char **p_argv;
  2151. const char *str;
  2152. for (p_argv = &ldd_argv[0]; (str = *p_argv) != (char *) 0; p_argv++)
  2153. fprintf (stderr, " %s", str);
  2154. fprintf (stderr, "\n");
  2155. }
  2156. fflush (stdout);
  2157. fflush (stderr);
  2158. pex = pex_init (PEX_USE_PIPES, "collect2", NULL);
  2159. if (pex == NULL)
  2160. fatal_error (input_location, "pex_init failed: %m");
  2161. errmsg = pex_run (pex, 0, ldd_file_name, real_ldd_argv, NULL, NULL, &err);
  2162. if (errmsg != NULL)
  2163. {
  2164. if (err != 0)
  2165. {
  2166. errno = err;
  2167. fatal_error (input_location, "%s: %m", _(errmsg));
  2168. }
  2169. else
  2170. fatal_error (input_location, errmsg);
  2171. }
  2172. int_handler = (void (*) (int)) signal (SIGINT, SIG_IGN);
  2173. #ifdef SIGQUIT
  2174. quit_handler = (void (*) (int)) signal (SIGQUIT, SIG_IGN);
  2175. #endif
  2176. inf = pex_read_output (pex, 0);
  2177. if (inf == NULL)
  2178. fatal_error (input_location, "can't open ldd output: %m");
  2179. if (debug)
  2180. notice ("\nldd output with constructors/destructors.\n");
  2181. /* Read each line of ldd output. */
  2182. while (fgets (buf, sizeof buf, inf) != (char *) 0)
  2183. {
  2184. int ch2;
  2185. char *name, *end, *p = buf;
  2186. /* Extract names of libraries and add to list. */
  2187. PARSE_LDD_OUTPUT (p);
  2188. if (p == 0)
  2189. continue;
  2190. name = p;
  2191. if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
  2192. fatal_error (input_location, "dynamic dependency %s not found", buf);
  2193. /* Find the end of the symbol name. */
  2194. for (end = p;
  2195. (ch2 = *end) != '\0' && ch2 != '\n' && !ISSPACE (ch2) && ch2 != '|';
  2196. end++)
  2197. continue;
  2198. *end = '\0';
  2199. if (access (name, R_OK) == 0)
  2200. add_to_list (&libraries, name);
  2201. else
  2202. fatal_error (input_location, "unable to open dynamic dependency '%s'",
  2203. buf);
  2204. if (debug)
  2205. fprintf (stderr, "\t%s\n", buf);
  2206. }
  2207. if (debug)
  2208. fprintf (stderr, "\n");
  2209. do_wait (ldd_file_name, pex);
  2210. signal (SIGINT, int_handler);
  2211. #ifdef SIGQUIT
  2212. signal (SIGQUIT, quit_handler);
  2213. #endif
  2214. /* Now iterate through the library list adding their symbols to
  2215. the list. */
  2216. for (list = libraries.first; list; list = list->next)
  2217. scan_prog_file (list->name, PASS_LIB, SCAN_ALL);
  2218. }
  2219. #endif /* LDD_SUFFIX */
  2220. #endif /* OBJECT_FORMAT_NONE */
  2221. /*
  2222. * COFF specific stuff.
  2223. */
  2224. #ifdef OBJECT_FORMAT_COFF
  2225. #if defined (EXTENDED_COFF)
  2226. # define GCC_SYMBOLS(X) (SYMHEADER (X).isymMax + SYMHEADER (X).iextMax)
  2227. # define GCC_SYMENT SYMR
  2228. # define GCC_OK_SYMBOL(X) ((X).st == stProc || (X).st == stGlobal)
  2229. # define GCC_SYMINC(X) (1)
  2230. # define GCC_SYMZERO(X) (SYMHEADER (X).isymMax)
  2231. # define GCC_CHECK_HDR(X) (PSYMTAB (X) != 0)
  2232. #else
  2233. # define GCC_SYMBOLS(X) (HEADER (ldptr).f_nsyms)
  2234. # define GCC_SYMENT SYMENT
  2235. # if defined (C_WEAKEXT)
  2236. # define GCC_OK_SYMBOL(X) \
  2237. (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
  2238. ((X).n_scnum > N_UNDEF) && \
  2239. (aix64_flag \
  2240. || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
  2241. || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
  2242. # define GCC_UNDEF_SYMBOL(X) \
  2243. (((X).n_sclass == C_EXT || (X).n_sclass == C_WEAKEXT) && \
  2244. ((X).n_scnum == N_UNDEF))
  2245. # else
  2246. # define GCC_OK_SYMBOL(X) \
  2247. (((X).n_sclass == C_EXT) && \
  2248. ((X).n_scnum > N_UNDEF) && \
  2249. (aix64_flag \
  2250. || (((X).n_type & N_TMASK) == (DT_NON << N_BTSHFT) \
  2251. || ((X).n_type & N_TMASK) == (DT_FCN << N_BTSHFT))))
  2252. # define GCC_UNDEF_SYMBOL(X) \
  2253. (((X).n_sclass == C_EXT) && ((X).n_scnum == N_UNDEF))
  2254. # endif
  2255. # define GCC_SYMINC(X) ((X).n_numaux+1)
  2256. # define GCC_SYMZERO(X) 0
  2257. /* 0757 = U803XTOCMAGIC (AIX 4.3) and 0767 = U64_TOCMAGIC (AIX V5) */
  2258. #if TARGET_AIX_VERSION >= 51
  2259. # define GCC_CHECK_HDR(X) \
  2260. (((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
  2261. || (HEADER (X).f_magic == 0767 && aix64_flag)) \
  2262. && !(HEADER (X).f_flags & F_LOADONLY))
  2263. #else
  2264. # define GCC_CHECK_HDR(X) \
  2265. (((HEADER (X).f_magic == U802TOCMAGIC && ! aix64_flag) \
  2266. || (HEADER (X).f_magic == 0757 && aix64_flag)) \
  2267. && !(HEADER (X).f_flags & F_LOADONLY))
  2268. #endif
  2269. #endif
  2270. #ifdef COLLECT_EXPORT_LIST
  2271. /* Array of standard AIX libraries which should not
  2272. be scanned for ctors/dtors. */
  2273. static const char *const aix_std_libs[] = {
  2274. "/unix",
  2275. "/lib/libc.a",
  2276. "/lib/libm.a",
  2277. "/lib/libc_r.a",
  2278. "/lib/libm_r.a",
  2279. "/usr/lib/libc.a",
  2280. "/usr/lib/libm.a",
  2281. "/usr/lib/libc_r.a",
  2282. "/usr/lib/libm_r.a",
  2283. "/usr/lib/threads/libc.a",
  2284. "/usr/ccs/lib/libc.a",
  2285. "/usr/ccs/lib/libm.a",
  2286. "/usr/ccs/lib/libc_r.a",
  2287. "/usr/ccs/lib/libm_r.a",
  2288. NULL
  2289. };
  2290. /* This function checks the filename and returns 1
  2291. if this name matches the location of a standard AIX library. */
  2292. static int ignore_library (const char *);
  2293. static int
  2294. ignore_library (const char *name)
  2295. {
  2296. const char *const *p;
  2297. size_t length;
  2298. if (target_system_root[0] != '\0')
  2299. {
  2300. length = strlen (target_system_root);
  2301. if (strncmp (name, target_system_root, length) != 0)
  2302. return 0;
  2303. name += length;
  2304. }
  2305. for (p = &aix_std_libs[0]; *p != NULL; ++p)
  2306. if (strcmp (name, *p) == 0)
  2307. return 1;
  2308. return 0;
  2309. }
  2310. #endif /* COLLECT_EXPORT_LIST */
  2311. #if defined (HAVE_DECL_LDGETNAME) && !HAVE_DECL_LDGETNAME
  2312. extern char *ldgetname (LDFILE *, GCC_SYMENT *);
  2313. #endif
  2314. /* COFF version to scan the name list of the loaded program for
  2315. the symbols g++ uses for static constructors and destructors. */
  2316. static void
  2317. scan_prog_file (const char *prog_name, scanpass which_pass,
  2318. scanfilter filter)
  2319. {
  2320. LDFILE *ldptr = NULL;
  2321. int sym_index, sym_count;
  2322. int is_shared = 0;
  2323. if (which_pass != PASS_FIRST && which_pass != PASS_OBJ)
  2324. return;
  2325. #ifdef COLLECT_EXPORT_LIST
  2326. /* We do not need scanning for some standard C libraries. */
  2327. if (which_pass == PASS_FIRST && ignore_library (prog_name))
  2328. return;
  2329. /* On AIX we have a loop, because there is not much difference
  2330. between an object and an archive. This trick allows us to
  2331. eliminate scan_libraries() function. */
  2332. do
  2333. {
  2334. #endif
  2335. /* Some platforms (e.g. OSF4) declare ldopen as taking a
  2336. non-const char * filename parameter, even though it will not
  2337. modify that string. So we must cast away const-ness here,
  2338. using CONST_CAST to prevent complaints from -Wcast-qual. */
  2339. if ((ldptr = ldopen (CONST_CAST (char *, prog_name), ldptr)) != NULL)
  2340. {
  2341. if (! MY_ISCOFF (HEADER (ldptr).f_magic))
  2342. fatal_error (input_location, "%s: not a COFF file", prog_name);
  2343. if (GCC_CHECK_HDR (ldptr))
  2344. {
  2345. sym_count = GCC_SYMBOLS (ldptr);
  2346. sym_index = GCC_SYMZERO (ldptr);
  2347. #ifdef COLLECT_EXPORT_LIST
  2348. /* Is current archive member a shared object? */
  2349. is_shared = HEADER (ldptr).f_flags & F_SHROBJ;
  2350. #endif
  2351. while (sym_index < sym_count)
  2352. {
  2353. GCC_SYMENT symbol;
  2354. if (ldtbread (ldptr, sym_index, &symbol) <= 0)
  2355. break;
  2356. sym_index += GCC_SYMINC (symbol);
  2357. if (GCC_OK_SYMBOL (symbol))
  2358. {
  2359. char *name;
  2360. if ((name = ldgetname (ldptr, &symbol)) == NULL)
  2361. continue; /* Should never happen. */
  2362. #ifdef XCOFF_DEBUGGING_INFO
  2363. /* All AIX function names have a duplicate entry
  2364. beginning with a dot. */
  2365. if (*name == '.')
  2366. ++name;
  2367. #endif
  2368. switch (is_ctor_dtor (name))
  2369. {
  2370. #if TARGET_AIX_VERSION
  2371. /* Add AIX shared library initalisers/finalisers
  2372. to the constructors/destructors list of the
  2373. current module. */
  2374. case SYM_AIXI:
  2375. if (! (filter & SCAN_CTOR))
  2376. break;
  2377. if (is_shared && !aixlazy_flag)
  2378. add_to_list (&constructors, name);
  2379. break;
  2380. case SYM_AIXD:
  2381. if (! (filter & SCAN_DTOR))
  2382. break;
  2383. if (is_shared && !aixlazy_flag)
  2384. add_to_list (&destructors, name);
  2385. break;
  2386. #endif
  2387. case SYM_CTOR:
  2388. if (! (filter & SCAN_CTOR))
  2389. break;
  2390. if (! is_shared)
  2391. add_to_list (&constructors, name);
  2392. #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
  2393. if (which_pass == PASS_OBJ)
  2394. add_to_list (&exports, name);
  2395. #endif
  2396. break;
  2397. case SYM_DTOR:
  2398. if (! (filter & SCAN_DTOR))
  2399. break;
  2400. if (! is_shared)
  2401. add_to_list (&destructors, name);
  2402. #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
  2403. if (which_pass == PASS_OBJ)
  2404. add_to_list (&exports, name);
  2405. #endif
  2406. break;
  2407. #ifdef COLLECT_EXPORT_LIST
  2408. case SYM_INIT:
  2409. if (! (filter & SCAN_INIT))
  2410. break;
  2411. #ifndef LD_INIT_SWITCH
  2412. if (is_shared)
  2413. add_to_list (&constructors, name);
  2414. #endif
  2415. break;
  2416. case SYM_FINI:
  2417. if (! (filter & SCAN_FINI))
  2418. break;
  2419. #ifndef LD_INIT_SWITCH
  2420. if (is_shared)
  2421. add_to_list (&destructors, name);
  2422. #endif
  2423. break;
  2424. #endif
  2425. case SYM_DWEH:
  2426. if (! (filter & SCAN_DWEH))
  2427. break;
  2428. if (! is_shared)
  2429. add_to_list (&frame_tables, name);
  2430. #if defined (COLLECT_EXPORT_LIST) && !defined (LD_INIT_SWITCH)
  2431. if (which_pass == PASS_OBJ)
  2432. add_to_list (&exports, name);
  2433. #endif
  2434. break;
  2435. default: /* not a constructor or destructor */
  2436. #ifdef COLLECT_EXPORT_LIST
  2437. /* Explicitly export all global symbols when
  2438. building a shared object on AIX, but do not
  2439. re-export symbols from another shared object
  2440. and do not export symbols if the user
  2441. provides an explicit export list. */
  2442. if (shared_obj && !is_shared
  2443. && which_pass == PASS_OBJ && !export_flag)
  2444. add_to_list (&exports, name);
  2445. #endif
  2446. continue;
  2447. }
  2448. if (debug)
  2449. #if !defined(EXTENDED_COFF)
  2450. fprintf (stderr, "\tsec=%d class=%d type=%s%o %s\n",
  2451. symbol.n_scnum, symbol.n_sclass,
  2452. (symbol.n_type ? "0" : ""), symbol.n_type,
  2453. name);
  2454. #else
  2455. fprintf (stderr,
  2456. "\tiss = %5d, value = %5ld, index = %5d, name = %s\n",
  2457. symbol.iss, (long) symbol.value, symbol.index, name);
  2458. #endif
  2459. }
  2460. }
  2461. }
  2462. #ifdef COLLECT_EXPORT_LIST
  2463. else
  2464. {
  2465. /* If archive contains both 32-bit and 64-bit objects,
  2466. we want to skip objects in other mode so mismatch normal. */
  2467. if (debug)
  2468. fprintf (stderr, "%s : magic=%o aix64=%d mismatch\n",
  2469. prog_name, HEADER (ldptr).f_magic, aix64_flag);
  2470. }
  2471. #endif
  2472. }
  2473. else
  2474. {
  2475. fatal_error (input_location, "%s: cannot open as COFF file",
  2476. prog_name);
  2477. }
  2478. #ifdef COLLECT_EXPORT_LIST
  2479. /* On AIX loop continues while there are more members in archive. */
  2480. }
  2481. while (ldclose (ldptr) == FAILURE);
  2482. #else
  2483. /* Otherwise we simply close ldptr. */
  2484. (void) ldclose (ldptr);
  2485. #endif
  2486. }
  2487. #endif /* OBJECT_FORMAT_COFF */
  2488. #ifdef COLLECT_EXPORT_LIST
  2489. /* Given a library name without "lib" prefix, this function
  2490. returns a full library name including a path. */
  2491. static char *
  2492. resolve_lib_name (const char *name)
  2493. {
  2494. char *lib_buf;
  2495. int i, j, l = 0;
  2496. /* Library extensions for AIX dynamic linking. */
  2497. const char * const libexts[2] = {"a", "so"};
  2498. for (i = 0; libpaths[i]; i++)
  2499. if (libpaths[i]->max_len > l)
  2500. l = libpaths[i]->max_len;
  2501. lib_buf = XNEWVEC (char, l + strlen (name) + 10);
  2502. for (i = 0; libpaths[i]; i++)
  2503. {
  2504. struct prefix_list *list = libpaths[i]->plist;
  2505. for (; list; list = list->next)
  2506. {
  2507. /* The following lines are needed because path_prefix list
  2508. may contain directories both with trailing DIR_SEPARATOR and
  2509. without it. */
  2510. const char *p = "";
  2511. if (!IS_DIR_SEPARATOR (list->prefix[strlen (list->prefix)-1]))
  2512. p = "/";
  2513. for (j = 0; j < 2; j++)
  2514. {
  2515. sprintf (lib_buf, "%s%slib%s.%s",
  2516. list->prefix, p, name,
  2517. libexts[(j + aixrtl_flag) % 2]);
  2518. if (debug) fprintf (stderr, "searching for: %s\n", lib_buf);
  2519. if (file_exists (lib_buf))
  2520. {
  2521. if (debug) fprintf (stderr, "found: %s\n", lib_buf);
  2522. return (lib_buf);
  2523. }
  2524. }
  2525. }
  2526. }
  2527. if (debug)
  2528. fprintf (stderr, "not found\n");
  2529. else
  2530. fatal_error (input_location, "library lib%s not found", name);
  2531. return (NULL);
  2532. }
  2533. #endif /* COLLECT_EXPORT_LIST */
  2534. #ifdef COLLECT_RUN_DSYMUTIL
  2535. static int flag_dsym = false;
  2536. static int flag_idsym = false;
  2537. static void
  2538. process_args (int *argcp, char **argv) {
  2539. int i, j;
  2540. int argc = *argcp;
  2541. for (i=0; i<argc; ++i)
  2542. {
  2543. if (strcmp (argv[i], "-dsym") == 0)
  2544. {
  2545. flag_dsym = true;
  2546. /* Remove the flag, as we handle all processing for it. */
  2547. j = i;
  2548. do
  2549. argv[j] = argv[j+1];
  2550. while (++j < argc);
  2551. --i;
  2552. argc = --(*argcp);
  2553. }
  2554. else if (strcmp (argv[i], "-idsym") == 0)
  2555. {
  2556. flag_idsym = true;
  2557. /* Remove the flag, as we handle all processing for it. */
  2558. j = i;
  2559. do
  2560. argv[j] = argv[j+1];
  2561. while (++j < argc);
  2562. --i;
  2563. argc = --(*argcp);
  2564. }
  2565. }
  2566. }
  2567. static void
  2568. do_dsymutil (const char *output_file) {
  2569. const char *dsymutil = DSYMUTIL + 1;
  2570. struct pex_obj *pex;
  2571. char **real_argv = XCNEWVEC (char *, 3);
  2572. const char ** argv = CONST_CAST2 (const char **, char **,
  2573. real_argv);
  2574. argv[0] = dsymutil;
  2575. argv[1] = output_file;
  2576. argv[2] = (char *) 0;
  2577. pex = collect_execute (dsymutil, real_argv, NULL, NULL,
  2578. PEX_LAST | PEX_SEARCH, false);
  2579. do_wait (dsymutil, pex);
  2580. }
  2581. static void
  2582. post_ld_pass (bool temp_file) {
  2583. if (!(temp_file && flag_idsym) && !flag_dsym)
  2584. return;
  2585. do_dsymutil (output_file);
  2586. }
  2587. #else
  2588. static void
  2589. process_args (int *argcp ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { }
  2590. static void post_ld_pass (bool temp_file ATTRIBUTE_UNUSED) { }
  2591. #endif