script.cc 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419
  1. // script.cc -- handle linker scripts for gold.
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstdio>
  19. #include <cstdlib>
  20. #include <cstring>
  21. #include <fnmatch.h>
  22. #include <string>
  23. #include <vector>
  24. #include "filenames.h"
  25. #include "elfcpp.h"
  26. #include "demangle.h"
  27. #include "dirsearch.h"
  28. #include "options.h"
  29. #include "fileread.h"
  30. #include "workqueue.h"
  31. #include "readsyms.h"
  32. #include "parameters.h"
  33. #include "layout.h"
  34. #include "symtab.h"
  35. #include "target-select.h"
  36. #include "script.h"
  37. #include "script-c.h"
  38. #include "incremental.h"
  39. namespace gold
  40. {
  41. // A token read from a script file. We don't implement keywords here;
  42. // all keywords are simply represented as a string.
  43. class Token
  44. {
  45. public:
  46. // Token classification.
  47. enum Classification
  48. {
  49. // Token is invalid.
  50. TOKEN_INVALID,
  51. // Token indicates end of input.
  52. TOKEN_EOF,
  53. // Token is a string of characters.
  54. TOKEN_STRING,
  55. // Token is a quoted string of characters.
  56. TOKEN_QUOTED_STRING,
  57. // Token is an operator.
  58. TOKEN_OPERATOR,
  59. // Token is a number (an integer).
  60. TOKEN_INTEGER
  61. };
  62. // We need an empty constructor so that we can put this STL objects.
  63. Token()
  64. : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
  65. opcode_(0), lineno_(0), charpos_(0)
  66. { }
  67. // A general token with no value.
  68. Token(Classification classification, int lineno, int charpos)
  69. : classification_(classification), value_(NULL), value_length_(0),
  70. opcode_(0), lineno_(lineno), charpos_(charpos)
  71. {
  72. gold_assert(classification == TOKEN_INVALID
  73. || classification == TOKEN_EOF);
  74. }
  75. // A general token with a value.
  76. Token(Classification classification, const char* value, size_t length,
  77. int lineno, int charpos)
  78. : classification_(classification), value_(value), value_length_(length),
  79. opcode_(0), lineno_(lineno), charpos_(charpos)
  80. {
  81. gold_assert(classification != TOKEN_INVALID
  82. && classification != TOKEN_EOF);
  83. }
  84. // A token representing an operator.
  85. Token(int opcode, int lineno, int charpos)
  86. : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
  87. opcode_(opcode), lineno_(lineno), charpos_(charpos)
  88. { }
  89. // Return whether the token is invalid.
  90. bool
  91. is_invalid() const
  92. { return this->classification_ == TOKEN_INVALID; }
  93. // Return whether this is an EOF token.
  94. bool
  95. is_eof() const
  96. { return this->classification_ == TOKEN_EOF; }
  97. // Return the token classification.
  98. Classification
  99. classification() const
  100. { return this->classification_; }
  101. // Return the line number at which the token starts.
  102. int
  103. lineno() const
  104. { return this->lineno_; }
  105. // Return the character position at this the token starts.
  106. int
  107. charpos() const
  108. { return this->charpos_; }
  109. // Get the value of a token.
  110. const char*
  111. string_value(size_t* length) const
  112. {
  113. gold_assert(this->classification_ == TOKEN_STRING
  114. || this->classification_ == TOKEN_QUOTED_STRING);
  115. *length = this->value_length_;
  116. return this->value_;
  117. }
  118. int
  119. operator_value() const
  120. {
  121. gold_assert(this->classification_ == TOKEN_OPERATOR);
  122. return this->opcode_;
  123. }
  124. uint64_t
  125. integer_value() const;
  126. private:
  127. // The token classification.
  128. Classification classification_;
  129. // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
  130. // TOKEN_INTEGER.
  131. const char* value_;
  132. // The length of the token value.
  133. size_t value_length_;
  134. // The token value, for TOKEN_OPERATOR.
  135. int opcode_;
  136. // The line number where this token started (one based).
  137. int lineno_;
  138. // The character position within the line where this token started
  139. // (one based).
  140. int charpos_;
  141. };
  142. // Return the value of a TOKEN_INTEGER.
  143. uint64_t
  144. Token::integer_value() const
  145. {
  146. gold_assert(this->classification_ == TOKEN_INTEGER);
  147. size_t len = this->value_length_;
  148. uint64_t multiplier = 1;
  149. char last = this->value_[len - 1];
  150. if (last == 'm' || last == 'M')
  151. {
  152. multiplier = 1024 * 1024;
  153. --len;
  154. }
  155. else if (last == 'k' || last == 'K')
  156. {
  157. multiplier = 1024;
  158. --len;
  159. }
  160. char *end;
  161. uint64_t ret = strtoull(this->value_, &end, 0);
  162. gold_assert(static_cast<size_t>(end - this->value_) == len);
  163. return ret * multiplier;
  164. }
  165. // This class handles lexing a file into a sequence of tokens.
  166. class Lex
  167. {
  168. public:
  169. // We unfortunately have to support different lexing modes, because
  170. // when reading different parts of a linker script we need to parse
  171. // things differently.
  172. enum Mode
  173. {
  174. // Reading an ordinary linker script.
  175. LINKER_SCRIPT,
  176. // Reading an expression in a linker script.
  177. EXPRESSION,
  178. // Reading a version script.
  179. VERSION_SCRIPT,
  180. // Reading a --dynamic-list file.
  181. DYNAMIC_LIST
  182. };
  183. Lex(const char* input_string, size_t input_length, int parsing_token)
  184. : input_string_(input_string), input_length_(input_length),
  185. current_(input_string), mode_(LINKER_SCRIPT),
  186. first_token_(parsing_token), token_(),
  187. lineno_(1), linestart_(input_string)
  188. { }
  189. // Read a file into a string.
  190. static void
  191. read_file(Input_file*, std::string*);
  192. // Return the next token.
  193. const Token*
  194. next_token();
  195. // Return the current lexing mode.
  196. Lex::Mode
  197. mode() const
  198. { return this->mode_; }
  199. // Set the lexing mode.
  200. void
  201. set_mode(Mode mode)
  202. { this->mode_ = mode; }
  203. private:
  204. Lex(const Lex&);
  205. Lex& operator=(const Lex&);
  206. // Make a general token with no value at the current location.
  207. Token
  208. make_token(Token::Classification c, const char* start) const
  209. { return Token(c, this->lineno_, start - this->linestart_ + 1); }
  210. // Make a general token with a value at the current location.
  211. Token
  212. make_token(Token::Classification c, const char* v, size_t len,
  213. const char* start)
  214. const
  215. { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
  216. // Make an operator token at the current location.
  217. Token
  218. make_token(int opcode, const char* start) const
  219. { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
  220. // Make an invalid token at the current location.
  221. Token
  222. make_invalid_token(const char* start)
  223. { return this->make_token(Token::TOKEN_INVALID, start); }
  224. // Make an EOF token at the current location.
  225. Token
  226. make_eof_token(const char* start)
  227. { return this->make_token(Token::TOKEN_EOF, start); }
  228. // Return whether C can be the first character in a name. C2 is the
  229. // next character, since we sometimes need that.
  230. inline bool
  231. can_start_name(char c, char c2);
  232. // If C can appear in a name which has already started, return a
  233. // pointer to a character later in the token or just past
  234. // it. Otherwise, return NULL.
  235. inline const char*
  236. can_continue_name(const char* c);
  237. // Return whether C, C2, C3 can start a hex number.
  238. inline bool
  239. can_start_hex(char c, char c2, char c3);
  240. // If C can appear in a hex number which has already started, return
  241. // a pointer to a character later in the token or just past
  242. // it. Otherwise, return NULL.
  243. inline const char*
  244. can_continue_hex(const char* c);
  245. // Return whether C can start a non-hex number.
  246. static inline bool
  247. can_start_number(char c);
  248. // If C can appear in a decimal number which has already started,
  249. // return a pointer to a character later in the token or just past
  250. // it. Otherwise, return NULL.
  251. inline const char*
  252. can_continue_number(const char* c)
  253. { return Lex::can_start_number(*c) ? c + 1 : NULL; }
  254. // If C1 C2 C3 form a valid three character operator, return the
  255. // opcode. Otherwise return 0.
  256. static inline int
  257. three_char_operator(char c1, char c2, char c3);
  258. // If C1 C2 form a valid two character operator, return the opcode.
  259. // Otherwise return 0.
  260. static inline int
  261. two_char_operator(char c1, char c2);
  262. // If C1 is a valid one character operator, return the opcode.
  263. // Otherwise return 0.
  264. static inline int
  265. one_char_operator(char c1);
  266. // Read the next token.
  267. Token
  268. get_token(const char**);
  269. // Skip a C style /* */ comment. Return false if the comment did
  270. // not end.
  271. bool
  272. skip_c_comment(const char**);
  273. // Skip a line # comment. Return false if there was no newline.
  274. bool
  275. skip_line_comment(const char**);
  276. // Build a token CLASSIFICATION from all characters that match
  277. // CAN_CONTINUE_FN. The token starts at START. Start matching from
  278. // MATCH. Set *PP to the character following the token.
  279. inline Token
  280. gather_token(Token::Classification,
  281. const char* (Lex::*can_continue_fn)(const char*),
  282. const char* start, const char* match, const char** pp);
  283. // Build a token from a quoted string.
  284. Token
  285. gather_quoted_string(const char** pp);
  286. // The string we are tokenizing.
  287. const char* input_string_;
  288. // The length of the string.
  289. size_t input_length_;
  290. // The current offset into the string.
  291. const char* current_;
  292. // The current lexing mode.
  293. Mode mode_;
  294. // The code to use for the first token. This is set to 0 after it
  295. // is used.
  296. int first_token_;
  297. // The current token.
  298. Token token_;
  299. // The current line number.
  300. int lineno_;
  301. // The start of the current line in the string.
  302. const char* linestart_;
  303. };
  304. // Read the whole file into memory. We don't expect linker scripts to
  305. // be large, so we just use a std::string as a buffer. We ignore the
  306. // data we've already read, so that we read aligned buffers.
  307. void
  308. Lex::read_file(Input_file* input_file, std::string* contents)
  309. {
  310. off_t filesize = input_file->file().filesize();
  311. contents->clear();
  312. contents->reserve(filesize);
  313. off_t off = 0;
  314. unsigned char buf[BUFSIZ];
  315. while (off < filesize)
  316. {
  317. off_t get = BUFSIZ;
  318. if (get > filesize - off)
  319. get = filesize - off;
  320. input_file->file().read(off, get, buf);
  321. contents->append(reinterpret_cast<char*>(&buf[0]), get);
  322. off += get;
  323. }
  324. }
  325. // Return whether C can be the start of a name, if the next character
  326. // is C2. A name can being with a letter, underscore, period, or
  327. // dollar sign. Because a name can be a file name, we also permit
  328. // forward slash, backslash, and tilde. Tilde is the tricky case
  329. // here; GNU ld also uses it as a bitwise not operator. It is only
  330. // recognized as the operator if it is not immediately followed by
  331. // some character which can appear in a symbol. That is, when we
  332. // don't know that we are looking at an expression, "~0" is a file
  333. // name, and "~ 0" is an expression using bitwise not. We are
  334. // compatible.
  335. inline bool
  336. Lex::can_start_name(char c, char c2)
  337. {
  338. switch (c)
  339. {
  340. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  341. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  342. case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
  343. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  344. case 'Y': case 'Z':
  345. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  346. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  347. case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
  348. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  349. case 'y': case 'z':
  350. case '_': case '.': case '$':
  351. return true;
  352. case '/': case '\\':
  353. return this->mode_ == LINKER_SCRIPT;
  354. case '~':
  355. return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2);
  356. case '*': case '[':
  357. return (this->mode_ == VERSION_SCRIPT
  358. || this->mode_ == DYNAMIC_LIST
  359. || (this->mode_ == LINKER_SCRIPT
  360. && can_continue_name(&c2)));
  361. default:
  362. return false;
  363. }
  364. }
  365. // Return whether C can continue a name which has already started.
  366. // Subsequent characters in a name are the same as the leading
  367. // characters, plus digits and "=+-:[],?*". So in general the linker
  368. // script language requires spaces around operators, unless we know
  369. // that we are parsing an expression.
  370. inline const char*
  371. Lex::can_continue_name(const char* c)
  372. {
  373. switch (*c)
  374. {
  375. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  376. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  377. case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
  378. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  379. case 'Y': case 'Z':
  380. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  381. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  382. case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
  383. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  384. case 'y': case 'z':
  385. case '_': case '.': case '$':
  386. case '0': case '1': case '2': case '3': case '4':
  387. case '5': case '6': case '7': case '8': case '9':
  388. return c + 1;
  389. // TODO(csilvers): why not allow ~ in names for version-scripts?
  390. case '/': case '\\': case '~':
  391. case '=': case '+':
  392. case ',':
  393. if (this->mode_ == LINKER_SCRIPT)
  394. return c + 1;
  395. return NULL;
  396. case '[': case ']': case '*': case '?': case '-':
  397. if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT
  398. || this->mode_ == DYNAMIC_LIST)
  399. return c + 1;
  400. return NULL;
  401. // TODO(csilvers): why allow this? ^ is meaningless in version scripts.
  402. case '^':
  403. if (this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST)
  404. return c + 1;
  405. return NULL;
  406. case ':':
  407. if (this->mode_ == LINKER_SCRIPT)
  408. return c + 1;
  409. else if ((this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST)
  410. && (c[1] == ':'))
  411. {
  412. // A name can have '::' in it, as that's a c++ namespace
  413. // separator. But a single colon is not part of a name.
  414. return c + 2;
  415. }
  416. return NULL;
  417. default:
  418. return NULL;
  419. }
  420. }
  421. // For a number we accept 0x followed by hex digits, or any sequence
  422. // of digits. The old linker accepts leading '$' for hex, and
  423. // trailing HXBOD. Those are for MRI compatibility and we don't
  424. // accept them.
  425. // Return whether C1 C2 C3 can start a hex number.
  426. inline bool
  427. Lex::can_start_hex(char c1, char c2, char c3)
  428. {
  429. if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
  430. return this->can_continue_hex(&c3);
  431. return false;
  432. }
  433. // Return whether C can appear in a hex number.
  434. inline const char*
  435. Lex::can_continue_hex(const char* c)
  436. {
  437. switch (*c)
  438. {
  439. case '0': case '1': case '2': case '3': case '4':
  440. case '5': case '6': case '7': case '8': case '9':
  441. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  442. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  443. return c + 1;
  444. default:
  445. return NULL;
  446. }
  447. }
  448. // Return whether C can start a non-hex number.
  449. inline bool
  450. Lex::can_start_number(char c)
  451. {
  452. switch (c)
  453. {
  454. case '0': case '1': case '2': case '3': case '4':
  455. case '5': case '6': case '7': case '8': case '9':
  456. return true;
  457. default:
  458. return false;
  459. }
  460. }
  461. // If C1 C2 C3 form a valid three character operator, return the
  462. // opcode (defined in the yyscript.h file generated from yyscript.y).
  463. // Otherwise return 0.
  464. inline int
  465. Lex::three_char_operator(char c1, char c2, char c3)
  466. {
  467. switch (c1)
  468. {
  469. case '<':
  470. if (c2 == '<' && c3 == '=')
  471. return LSHIFTEQ;
  472. break;
  473. case '>':
  474. if (c2 == '>' && c3 == '=')
  475. return RSHIFTEQ;
  476. break;
  477. default:
  478. break;
  479. }
  480. return 0;
  481. }
  482. // If C1 C2 form a valid two character operator, return the opcode
  483. // (defined in the yyscript.h file generated from yyscript.y).
  484. // Otherwise return 0.
  485. inline int
  486. Lex::two_char_operator(char c1, char c2)
  487. {
  488. switch (c1)
  489. {
  490. case '=':
  491. if (c2 == '=')
  492. return EQ;
  493. break;
  494. case '!':
  495. if (c2 == '=')
  496. return NE;
  497. break;
  498. case '+':
  499. if (c2 == '=')
  500. return PLUSEQ;
  501. break;
  502. case '-':
  503. if (c2 == '=')
  504. return MINUSEQ;
  505. break;
  506. case '*':
  507. if (c2 == '=')
  508. return MULTEQ;
  509. break;
  510. case '/':
  511. if (c2 == '=')
  512. return DIVEQ;
  513. break;
  514. case '|':
  515. if (c2 == '=')
  516. return OREQ;
  517. if (c2 == '|')
  518. return OROR;
  519. break;
  520. case '&':
  521. if (c2 == '=')
  522. return ANDEQ;
  523. if (c2 == '&')
  524. return ANDAND;
  525. break;
  526. case '>':
  527. if (c2 == '=')
  528. return GE;
  529. if (c2 == '>')
  530. return RSHIFT;
  531. break;
  532. case '<':
  533. if (c2 == '=')
  534. return LE;
  535. if (c2 == '<')
  536. return LSHIFT;
  537. break;
  538. default:
  539. break;
  540. }
  541. return 0;
  542. }
  543. // If C1 is a valid operator, return the opcode. Otherwise return 0.
  544. inline int
  545. Lex::one_char_operator(char c1)
  546. {
  547. switch (c1)
  548. {
  549. case '+':
  550. case '-':
  551. case '*':
  552. case '/':
  553. case '%':
  554. case '!':
  555. case '&':
  556. case '|':
  557. case '^':
  558. case '~':
  559. case '<':
  560. case '>':
  561. case '=':
  562. case '?':
  563. case ',':
  564. case '(':
  565. case ')':
  566. case '{':
  567. case '}':
  568. case '[':
  569. case ']':
  570. case ':':
  571. case ';':
  572. return c1;
  573. default:
  574. return 0;
  575. }
  576. }
  577. // Skip a C style comment. *PP points to just after the "/*". Return
  578. // false if the comment did not end.
  579. bool
  580. Lex::skip_c_comment(const char** pp)
  581. {
  582. const char* p = *pp;
  583. while (p[0] != '*' || p[1] != '/')
  584. {
  585. if (*p == '\0')
  586. {
  587. *pp = p;
  588. return false;
  589. }
  590. if (*p == '\n')
  591. {
  592. ++this->lineno_;
  593. this->linestart_ = p + 1;
  594. }
  595. ++p;
  596. }
  597. *pp = p + 2;
  598. return true;
  599. }
  600. // Skip a line # comment. Return false if there was no newline.
  601. bool
  602. Lex::skip_line_comment(const char** pp)
  603. {
  604. const char* p = *pp;
  605. size_t skip = strcspn(p, "\n");
  606. if (p[skip] == '\0')
  607. {
  608. *pp = p + skip;
  609. return false;
  610. }
  611. p += skip + 1;
  612. ++this->lineno_;
  613. this->linestart_ = p;
  614. *pp = p;
  615. return true;
  616. }
  617. // Build a token CLASSIFICATION from all characters that match
  618. // CAN_CONTINUE_FN. Update *PP.
  619. inline Token
  620. Lex::gather_token(Token::Classification classification,
  621. const char* (Lex::*can_continue_fn)(const char*),
  622. const char* start,
  623. const char* match,
  624. const char** pp)
  625. {
  626. const char* new_match = NULL;
  627. while ((new_match = (this->*can_continue_fn)(match)) != NULL)
  628. match = new_match;
  629. // A special case: integers may be followed by a single M or K,
  630. // case-insensitive.
  631. if (classification == Token::TOKEN_INTEGER
  632. && (*match == 'm' || *match == 'M' || *match == 'k' || *match == 'K'))
  633. ++match;
  634. *pp = match;
  635. return this->make_token(classification, start, match - start, start);
  636. }
  637. // Build a token from a quoted string.
  638. Token
  639. Lex::gather_quoted_string(const char** pp)
  640. {
  641. const char* start = *pp;
  642. const char* p = start;
  643. ++p;
  644. size_t skip = strcspn(p, "\"\n");
  645. if (p[skip] != '"')
  646. return this->make_invalid_token(start);
  647. *pp = p + skip + 1;
  648. return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
  649. }
  650. // Return the next token at *PP. Update *PP. General guideline: we
  651. // require linker scripts to be simple ASCII. No unicode linker
  652. // scripts. In particular we can assume that any '\0' is the end of
  653. // the input.
  654. Token
  655. Lex::get_token(const char** pp)
  656. {
  657. const char* p = *pp;
  658. while (true)
  659. {
  660. if (*p == '\0')
  661. {
  662. *pp = p;
  663. return this->make_eof_token(p);
  664. }
  665. // Skip whitespace quickly.
  666. while (*p == ' ' || *p == '\t' || *p == '\r')
  667. ++p;
  668. if (*p == '\n')
  669. {
  670. ++p;
  671. ++this->lineno_;
  672. this->linestart_ = p;
  673. continue;
  674. }
  675. // Skip C style comments.
  676. if (p[0] == '/' && p[1] == '*')
  677. {
  678. int lineno = this->lineno_;
  679. int charpos = p - this->linestart_ + 1;
  680. *pp = p + 2;
  681. if (!this->skip_c_comment(pp))
  682. return Token(Token::TOKEN_INVALID, lineno, charpos);
  683. p = *pp;
  684. continue;
  685. }
  686. // Skip line comments.
  687. if (*p == '#')
  688. {
  689. *pp = p + 1;
  690. if (!this->skip_line_comment(pp))
  691. return this->make_eof_token(p);
  692. p = *pp;
  693. continue;
  694. }
  695. // Check for a name.
  696. if (this->can_start_name(p[0], p[1]))
  697. return this->gather_token(Token::TOKEN_STRING,
  698. &Lex::can_continue_name,
  699. p, p + 1, pp);
  700. // We accept any arbitrary name in double quotes, as long as it
  701. // does not cross a line boundary.
  702. if (*p == '"')
  703. {
  704. *pp = p;
  705. return this->gather_quoted_string(pp);
  706. }
  707. // Check for a number.
  708. if (this->can_start_hex(p[0], p[1], p[2]))
  709. return this->gather_token(Token::TOKEN_INTEGER,
  710. &Lex::can_continue_hex,
  711. p, p + 3, pp);
  712. if (Lex::can_start_number(p[0]))
  713. return this->gather_token(Token::TOKEN_INTEGER,
  714. &Lex::can_continue_number,
  715. p, p + 1, pp);
  716. // Check for operators.
  717. int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
  718. if (opcode != 0)
  719. {
  720. *pp = p + 3;
  721. return this->make_token(opcode, p);
  722. }
  723. opcode = Lex::two_char_operator(p[0], p[1]);
  724. if (opcode != 0)
  725. {
  726. *pp = p + 2;
  727. return this->make_token(opcode, p);
  728. }
  729. opcode = Lex::one_char_operator(p[0]);
  730. if (opcode != 0)
  731. {
  732. *pp = p + 1;
  733. return this->make_token(opcode, p);
  734. }
  735. return this->make_token(Token::TOKEN_INVALID, p);
  736. }
  737. }
  738. // Return the next token.
  739. const Token*
  740. Lex::next_token()
  741. {
  742. // The first token is special.
  743. if (this->first_token_ != 0)
  744. {
  745. this->token_ = Token(this->first_token_, 0, 0);
  746. this->first_token_ = 0;
  747. return &this->token_;
  748. }
  749. this->token_ = this->get_token(&this->current_);
  750. // Don't let an early null byte fool us into thinking that we've
  751. // reached the end of the file.
  752. if (this->token_.is_eof()
  753. && (static_cast<size_t>(this->current_ - this->input_string_)
  754. < this->input_length_))
  755. this->token_ = this->make_invalid_token(this->current_);
  756. return &this->token_;
  757. }
  758. // class Symbol_assignment.
  759. // Add the symbol to the symbol table. This makes sure the symbol is
  760. // there and defined. The actual value is stored later. We can't
  761. // determine the actual value at this point, because we can't
  762. // necessarily evaluate the expression until all ordinary symbols have
  763. // been finalized.
  764. // The GNU linker lets symbol assignments in the linker script
  765. // silently override defined symbols in object files. We are
  766. // compatible. FIXME: Should we issue a warning?
  767. void
  768. Symbol_assignment::add_to_table(Symbol_table* symtab)
  769. {
  770. elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
  771. this->sym_ = symtab->define_as_constant(this->name_.c_str(),
  772. NULL, // version
  773. (this->is_defsym_
  774. ? Symbol_table::DEFSYM
  775. : Symbol_table::SCRIPT),
  776. 0, // value
  777. 0, // size
  778. elfcpp::STT_NOTYPE,
  779. elfcpp::STB_GLOBAL,
  780. vis,
  781. 0, // nonvis
  782. this->provide_,
  783. true); // force_override
  784. }
  785. // Finalize a symbol value.
  786. void
  787. Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout)
  788. {
  789. this->finalize_maybe_dot(symtab, layout, false, 0, NULL);
  790. }
  791. // Finalize a symbol value which can refer to the dot symbol.
  792. void
  793. Symbol_assignment::finalize_with_dot(Symbol_table* symtab,
  794. const Layout* layout,
  795. uint64_t dot_value,
  796. Output_section* dot_section)
  797. {
  798. this->finalize_maybe_dot(symtab, layout, true, dot_value, dot_section);
  799. }
  800. // Finalize a symbol value, internal version.
  801. void
  802. Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab,
  803. const Layout* layout,
  804. bool is_dot_available,
  805. uint64_t dot_value,
  806. Output_section* dot_section)
  807. {
  808. // If we were only supposed to provide this symbol, the sym_ field
  809. // will be NULL if the symbol was not referenced.
  810. if (this->sym_ == NULL)
  811. {
  812. gold_assert(this->provide_);
  813. return;
  814. }
  815. if (parameters->target().get_size() == 32)
  816. {
  817. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  818. this->sized_finalize<32>(symtab, layout, is_dot_available, dot_value,
  819. dot_section);
  820. #else
  821. gold_unreachable();
  822. #endif
  823. }
  824. else if (parameters->target().get_size() == 64)
  825. {
  826. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  827. this->sized_finalize<64>(symtab, layout, is_dot_available, dot_value,
  828. dot_section);
  829. #else
  830. gold_unreachable();
  831. #endif
  832. }
  833. else
  834. gold_unreachable();
  835. }
  836. template<int size>
  837. void
  838. Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout,
  839. bool is_dot_available, uint64_t dot_value,
  840. Output_section* dot_section)
  841. {
  842. Output_section* section;
  843. elfcpp::STT type = elfcpp::STT_NOTYPE;
  844. elfcpp::STV vis = elfcpp::STV_DEFAULT;
  845. unsigned char nonvis = 0;
  846. uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout, true,
  847. is_dot_available,
  848. dot_value, dot_section,
  849. &section, NULL, &type,
  850. &vis, &nonvis, false, NULL);
  851. Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_);
  852. ssym->set_value(final_val);
  853. ssym->set_type(type);
  854. ssym->set_visibility(vis);
  855. ssym->set_nonvis(nonvis);
  856. if (section != NULL)
  857. ssym->set_output_section(section);
  858. }
  859. // Set the symbol value if the expression yields an absolute value or
  860. // a value relative to DOT_SECTION.
  861. void
  862. Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout,
  863. bool is_dot_available, uint64_t dot_value,
  864. Output_section* dot_section)
  865. {
  866. if (this->sym_ == NULL)
  867. return;
  868. Output_section* val_section;
  869. bool is_valid;
  870. uint64_t val = this->val_->eval_maybe_dot(symtab, layout, false,
  871. is_dot_available, dot_value,
  872. dot_section, &val_section, NULL,
  873. NULL, NULL, NULL, false, &is_valid);
  874. if (!is_valid || (val_section != NULL && val_section != dot_section))
  875. return;
  876. if (parameters->target().get_size() == 32)
  877. {
  878. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  879. Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_);
  880. ssym->set_value(val);
  881. #else
  882. gold_unreachable();
  883. #endif
  884. }
  885. else if (parameters->target().get_size() == 64)
  886. {
  887. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  888. Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_);
  889. ssym->set_value(val);
  890. #else
  891. gold_unreachable();
  892. #endif
  893. }
  894. else
  895. gold_unreachable();
  896. if (val_section != NULL)
  897. this->sym_->set_output_section(val_section);
  898. }
  899. // Print for debugging.
  900. void
  901. Symbol_assignment::print(FILE* f) const
  902. {
  903. if (this->provide_ && this->hidden_)
  904. fprintf(f, "PROVIDE_HIDDEN(");
  905. else if (this->provide_)
  906. fprintf(f, "PROVIDE(");
  907. else if (this->hidden_)
  908. gold_unreachable();
  909. fprintf(f, "%s = ", this->name_.c_str());
  910. this->val_->print(f);
  911. if (this->provide_ || this->hidden_)
  912. fprintf(f, ")");
  913. fprintf(f, "\n");
  914. }
  915. // Class Script_assertion.
  916. // Check the assertion.
  917. void
  918. Script_assertion::check(const Symbol_table* symtab, const Layout* layout)
  919. {
  920. if (!this->check_->eval(symtab, layout, true))
  921. gold_error("%s", this->message_.c_str());
  922. }
  923. // Print for debugging.
  924. void
  925. Script_assertion::print(FILE* f) const
  926. {
  927. fprintf(f, "ASSERT(");
  928. this->check_->print(f);
  929. fprintf(f, ", \"%s\")\n", this->message_.c_str());
  930. }
  931. // Class Script_options.
  932. Script_options::Script_options()
  933. : entry_(), symbol_assignments_(), symbol_definitions_(),
  934. symbol_references_(), version_script_info_(), script_sections_()
  935. {
  936. }
  937. // Returns true if NAME is on the list of symbol assignments waiting
  938. // to be processed.
  939. bool
  940. Script_options::is_pending_assignment(const char* name)
  941. {
  942. for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
  943. p != this->symbol_assignments_.end();
  944. ++p)
  945. if ((*p)->name() == name)
  946. return true;
  947. return false;
  948. }
  949. // Add a symbol to be defined.
  950. void
  951. Script_options::add_symbol_assignment(const char* name, size_t length,
  952. bool is_defsym, Expression* value,
  953. bool provide, bool hidden)
  954. {
  955. if (length != 1 || name[0] != '.')
  956. {
  957. if (this->script_sections_.in_sections_clause())
  958. {
  959. gold_assert(!is_defsym);
  960. this->script_sections_.add_symbol_assignment(name, length, value,
  961. provide, hidden);
  962. }
  963. else
  964. {
  965. Symbol_assignment* p = new Symbol_assignment(name, length, is_defsym,
  966. value, provide, hidden);
  967. this->symbol_assignments_.push_back(p);
  968. }
  969. if (!provide)
  970. {
  971. std::string n(name, length);
  972. this->symbol_definitions_.insert(n);
  973. this->symbol_references_.erase(n);
  974. }
  975. }
  976. else
  977. {
  978. if (provide || hidden)
  979. gold_error(_("invalid use of PROVIDE for dot symbol"));
  980. // The GNU linker permits assignments to dot outside of SECTIONS
  981. // clauses and treats them as occurring inside, so we don't
  982. // check in_sections_clause here.
  983. this->script_sections_.add_dot_assignment(value);
  984. }
  985. }
  986. // Add a reference to a symbol.
  987. void
  988. Script_options::add_symbol_reference(const char* name, size_t length)
  989. {
  990. if (length != 1 || name[0] != '.')
  991. {
  992. std::string n(name, length);
  993. if (this->symbol_definitions_.find(n) == this->symbol_definitions_.end())
  994. this->symbol_references_.insert(n);
  995. }
  996. }
  997. // Add an assertion.
  998. void
  999. Script_options::add_assertion(Expression* check, const char* message,
  1000. size_t messagelen)
  1001. {
  1002. if (this->script_sections_.in_sections_clause())
  1003. this->script_sections_.add_assertion(check, message, messagelen);
  1004. else
  1005. {
  1006. Script_assertion* p = new Script_assertion(check, message, messagelen);
  1007. this->assertions_.push_back(p);
  1008. }
  1009. }
  1010. // Create sections required by any linker scripts.
  1011. void
  1012. Script_options::create_script_sections(Layout* layout)
  1013. {
  1014. if (this->saw_sections_clause())
  1015. this->script_sections_.create_sections(layout);
  1016. }
  1017. // Add any symbols we are defining to the symbol table.
  1018. void
  1019. Script_options::add_symbols_to_table(Symbol_table* symtab)
  1020. {
  1021. for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
  1022. p != this->symbol_assignments_.end();
  1023. ++p)
  1024. (*p)->add_to_table(symtab);
  1025. this->script_sections_.add_symbols_to_table(symtab);
  1026. }
  1027. // Finalize symbol values. Also check assertions.
  1028. void
  1029. Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
  1030. {
  1031. // We finalize the symbols defined in SECTIONS first, because they
  1032. // are the ones which may have changed. This way if symbol outside
  1033. // SECTIONS are defined in terms of symbols inside SECTIONS, they
  1034. // will get the right value.
  1035. this->script_sections_.finalize_symbols(symtab, layout);
  1036. for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
  1037. p != this->symbol_assignments_.end();
  1038. ++p)
  1039. (*p)->finalize(symtab, layout);
  1040. for (Assertions::iterator p = this->assertions_.begin();
  1041. p != this->assertions_.end();
  1042. ++p)
  1043. (*p)->check(symtab, layout);
  1044. }
  1045. // Set section addresses. We set all the symbols which have absolute
  1046. // values. Then we let the SECTIONS clause do its thing. This
  1047. // returns the segment which holds the file header and segment
  1048. // headers, if any.
  1049. Output_segment*
  1050. Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout)
  1051. {
  1052. for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
  1053. p != this->symbol_assignments_.end();
  1054. ++p)
  1055. (*p)->set_if_absolute(symtab, layout, false, 0, NULL);
  1056. return this->script_sections_.set_section_addresses(symtab, layout);
  1057. }
  1058. // This class holds data passed through the parser to the lexer and to
  1059. // the parser support functions. This avoids global variables. We
  1060. // can't use global variables because we need not be called by a
  1061. // singleton thread.
  1062. class Parser_closure
  1063. {
  1064. public:
  1065. Parser_closure(const char* filename,
  1066. const Position_dependent_options& posdep_options,
  1067. bool parsing_defsym, bool in_group, bool is_in_sysroot,
  1068. Command_line* command_line,
  1069. Script_options* script_options,
  1070. Lex* lex,
  1071. bool skip_on_incompatible_target,
  1072. Script_info* script_info)
  1073. : filename_(filename), posdep_options_(posdep_options),
  1074. parsing_defsym_(parsing_defsym), in_group_(in_group),
  1075. is_in_sysroot_(is_in_sysroot),
  1076. skip_on_incompatible_target_(skip_on_incompatible_target),
  1077. found_incompatible_target_(false),
  1078. command_line_(command_line), script_options_(script_options),
  1079. version_script_info_(script_options->version_script_info()),
  1080. lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL),
  1081. script_info_(script_info)
  1082. {
  1083. // We start out processing C symbols in the default lex mode.
  1084. this->language_stack_.push_back(Version_script_info::LANGUAGE_C);
  1085. this->lex_mode_stack_.push_back(lex->mode());
  1086. }
  1087. // Return the file name.
  1088. const char*
  1089. filename() const
  1090. { return this->filename_; }
  1091. // Return the position dependent options. The caller may modify
  1092. // this.
  1093. Position_dependent_options&
  1094. position_dependent_options()
  1095. { return this->posdep_options_; }
  1096. // Whether we are parsing a --defsym.
  1097. bool
  1098. parsing_defsym() const
  1099. { return this->parsing_defsym_; }
  1100. // Return whether this script is being run in a group.
  1101. bool
  1102. in_group() const
  1103. { return this->in_group_; }
  1104. // Return whether this script was found using a directory in the
  1105. // sysroot.
  1106. bool
  1107. is_in_sysroot() const
  1108. { return this->is_in_sysroot_; }
  1109. // Whether to skip to the next file with the same name if we find an
  1110. // incompatible target in an OUTPUT_FORMAT statement.
  1111. bool
  1112. skip_on_incompatible_target() const
  1113. { return this->skip_on_incompatible_target_; }
  1114. // Stop skipping to the next file on an incompatible target. This
  1115. // is called when we make some unrevocable change to the data
  1116. // structures.
  1117. void
  1118. clear_skip_on_incompatible_target()
  1119. { this->skip_on_incompatible_target_ = false; }
  1120. // Whether we found an incompatible target in an OUTPUT_FORMAT
  1121. // statement.
  1122. bool
  1123. found_incompatible_target() const
  1124. { return this->found_incompatible_target_; }
  1125. // Note that we found an incompatible target.
  1126. void
  1127. set_found_incompatible_target()
  1128. { this->found_incompatible_target_ = true; }
  1129. // Returns the Command_line structure passed in at constructor time.
  1130. // This value may be NULL. The caller may modify this, which modifies
  1131. // the passed-in Command_line object (not a copy).
  1132. Command_line*
  1133. command_line()
  1134. { return this->command_line_; }
  1135. // Return the options which may be set by a script.
  1136. Script_options*
  1137. script_options()
  1138. { return this->script_options_; }
  1139. // Return the object in which version script information should be stored.
  1140. Version_script_info*
  1141. version_script()
  1142. { return this->version_script_info_; }
  1143. // Return the next token, and advance.
  1144. const Token*
  1145. next_token()
  1146. {
  1147. const Token* token = this->lex_->next_token();
  1148. this->lineno_ = token->lineno();
  1149. this->charpos_ = token->charpos();
  1150. return token;
  1151. }
  1152. // Set a new lexer mode, pushing the current one.
  1153. void
  1154. push_lex_mode(Lex::Mode mode)
  1155. {
  1156. this->lex_mode_stack_.push_back(this->lex_->mode());
  1157. this->lex_->set_mode(mode);
  1158. }
  1159. // Pop the lexer mode.
  1160. void
  1161. pop_lex_mode()
  1162. {
  1163. gold_assert(!this->lex_mode_stack_.empty());
  1164. this->lex_->set_mode(this->lex_mode_stack_.back());
  1165. this->lex_mode_stack_.pop_back();
  1166. }
  1167. // Return the current lexer mode.
  1168. Lex::Mode
  1169. lex_mode() const
  1170. { return this->lex_mode_stack_.back(); }
  1171. // Return the line number of the last token.
  1172. int
  1173. lineno() const
  1174. { return this->lineno_; }
  1175. // Return the character position in the line of the last token.
  1176. int
  1177. charpos() const
  1178. { return this->charpos_; }
  1179. // Return the list of input files, creating it if necessary. This
  1180. // is a space leak--we never free the INPUTS_ pointer.
  1181. Input_arguments*
  1182. inputs()
  1183. {
  1184. if (this->inputs_ == NULL)
  1185. this->inputs_ = new Input_arguments();
  1186. return this->inputs_;
  1187. }
  1188. // Return whether we saw any input files.
  1189. bool
  1190. saw_inputs() const
  1191. { return this->inputs_ != NULL && !this->inputs_->empty(); }
  1192. // Return the current language being processed in a version script
  1193. // (eg, "C++"). The empty string represents unmangled C names.
  1194. Version_script_info::Language
  1195. get_current_language() const
  1196. { return this->language_stack_.back(); }
  1197. // Push a language onto the stack when entering an extern block.
  1198. void
  1199. push_language(Version_script_info::Language lang)
  1200. { this->language_stack_.push_back(lang); }
  1201. // Pop a language off of the stack when exiting an extern block.
  1202. void
  1203. pop_language()
  1204. {
  1205. gold_assert(!this->language_stack_.empty());
  1206. this->language_stack_.pop_back();
  1207. }
  1208. // Return a pointer to the incremental info.
  1209. Script_info*
  1210. script_info()
  1211. { return this->script_info_; }
  1212. private:
  1213. // The name of the file we are reading.
  1214. const char* filename_;
  1215. // The position dependent options.
  1216. Position_dependent_options posdep_options_;
  1217. // True if we are parsing a --defsym.
  1218. bool parsing_defsym_;
  1219. // Whether we are currently in a --start-group/--end-group.
  1220. bool in_group_;
  1221. // Whether the script was found in a sysrooted directory.
  1222. bool is_in_sysroot_;
  1223. // If this is true, then if we find an OUTPUT_FORMAT with an
  1224. // incompatible target, then we tell the parser to abort so that we
  1225. // can search for the next file with the same name.
  1226. bool skip_on_incompatible_target_;
  1227. // True if we found an OUTPUT_FORMAT with an incompatible target.
  1228. bool found_incompatible_target_;
  1229. // May be NULL if the user chooses not to pass one in.
  1230. Command_line* command_line_;
  1231. // Options which may be set from any linker script.
  1232. Script_options* script_options_;
  1233. // Information parsed from a version script.
  1234. Version_script_info* version_script_info_;
  1235. // The lexer.
  1236. Lex* lex_;
  1237. // The line number of the last token returned by next_token.
  1238. int lineno_;
  1239. // The column number of the last token returned by next_token.
  1240. int charpos_;
  1241. // A stack of lexer modes.
  1242. std::vector<Lex::Mode> lex_mode_stack_;
  1243. // A stack of which extern/language block we're inside. Can be C++,
  1244. // java, or empty for C.
  1245. std::vector<Version_script_info::Language> language_stack_;
  1246. // New input files found to add to the link.
  1247. Input_arguments* inputs_;
  1248. // Pointer to incremental linking info.
  1249. Script_info* script_info_;
  1250. };
  1251. // FILE was found as an argument on the command line. Try to read it
  1252. // as a script. Return true if the file was handled.
  1253. bool
  1254. read_input_script(Workqueue* workqueue, Symbol_table* symtab, Layout* layout,
  1255. Dirsearch* dirsearch, int dirindex,
  1256. Input_objects* input_objects, Mapfile* mapfile,
  1257. Input_group* input_group,
  1258. const Input_argument* input_argument,
  1259. Input_file* input_file, Task_token* next_blocker,
  1260. bool* used_next_blocker)
  1261. {
  1262. *used_next_blocker = false;
  1263. std::string input_string;
  1264. Lex::read_file(input_file, &input_string);
  1265. Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
  1266. Script_info* script_info = NULL;
  1267. if (layout->incremental_inputs() != NULL)
  1268. {
  1269. const std::string& filename = input_file->filename();
  1270. Timespec mtime = input_file->file().get_mtime();
  1271. unsigned int arg_serial = input_argument->file().arg_serial();
  1272. script_info = new Script_info(filename);
  1273. layout->incremental_inputs()->report_script(script_info, arg_serial,
  1274. mtime);
  1275. }
  1276. Parser_closure closure(input_file->filename().c_str(),
  1277. input_argument->file().options(),
  1278. false,
  1279. input_group != NULL,
  1280. input_file->is_in_sysroot(),
  1281. NULL,
  1282. layout->script_options(),
  1283. &lex,
  1284. input_file->will_search_for(),
  1285. script_info);
  1286. bool old_saw_sections_clause =
  1287. layout->script_options()->saw_sections_clause();
  1288. if (yyparse(&closure) != 0)
  1289. {
  1290. if (closure.found_incompatible_target())
  1291. {
  1292. Read_symbols::incompatible_warning(input_argument, input_file);
  1293. Read_symbols::requeue(workqueue, input_objects, symtab, layout,
  1294. dirsearch, dirindex, mapfile, input_argument,
  1295. input_group, next_blocker);
  1296. return true;
  1297. }
  1298. return false;
  1299. }
  1300. if (!old_saw_sections_clause
  1301. && layout->script_options()->saw_sections_clause()
  1302. && layout->have_added_input_section())
  1303. gold_error(_("%s: SECTIONS seen after other input files; try -T/--script"),
  1304. input_file->filename().c_str());
  1305. if (!closure.saw_inputs())
  1306. return true;
  1307. Task_token* this_blocker = NULL;
  1308. for (Input_arguments::const_iterator p = closure.inputs()->begin();
  1309. p != closure.inputs()->end();
  1310. ++p)
  1311. {
  1312. Task_token* nb;
  1313. if (p + 1 == closure.inputs()->end())
  1314. nb = next_blocker;
  1315. else
  1316. {
  1317. nb = new Task_token(true);
  1318. nb->add_blocker();
  1319. }
  1320. workqueue->queue_soon(new Read_symbols(input_objects, symtab,
  1321. layout, dirsearch, 0, mapfile, &*p,
  1322. input_group, NULL, this_blocker, nb));
  1323. this_blocker = nb;
  1324. }
  1325. *used_next_blocker = true;
  1326. return true;
  1327. }
  1328. // Helper function for read_version_script(), read_commandline_script() and
  1329. // script_include_directive(). Processes the given file in the mode indicated
  1330. // by first_token and lex_mode.
  1331. static bool
  1332. read_script_file(const char* filename, Command_line* cmdline,
  1333. Script_options* script_options,
  1334. int first_token, Lex::Mode lex_mode)
  1335. {
  1336. Dirsearch dirsearch;
  1337. std::string name = filename;
  1338. // If filename is a relative filename, search for it manually using "." +
  1339. // cmdline->options()->library_path() -- not dirsearch.
  1340. if (!IS_ABSOLUTE_PATH(filename))
  1341. {
  1342. const General_options::Dir_list& search_path =
  1343. cmdline->options().library_path();
  1344. name = Dirsearch::find_file_in_dir_list(name, search_path, ".");
  1345. }
  1346. // The file locking code wants to record a Task, but we haven't
  1347. // started the workqueue yet. This is only for debugging purposes,
  1348. // so we invent a fake value.
  1349. const Task* task = reinterpret_cast<const Task*>(-1);
  1350. // We don't want this file to be opened in binary mode.
  1351. Position_dependent_options posdep = cmdline->position_dependent_options();
  1352. if (posdep.format_enum() == General_options::OBJECT_FORMAT_BINARY)
  1353. posdep.set_format_enum(General_options::OBJECT_FORMAT_ELF);
  1354. Input_file_argument input_argument(name.c_str(),
  1355. Input_file_argument::INPUT_FILE_TYPE_FILE,
  1356. "", false, posdep);
  1357. Input_file input_file(&input_argument);
  1358. int dummy = 0;
  1359. if (!input_file.open(dirsearch, task, &dummy))
  1360. return false;
  1361. std::string input_string;
  1362. Lex::read_file(&input_file, &input_string);
  1363. Lex lex(input_string.c_str(), input_string.length(), first_token);
  1364. lex.set_mode(lex_mode);
  1365. Parser_closure closure(filename,
  1366. cmdline->position_dependent_options(),
  1367. first_token == Lex::DYNAMIC_LIST,
  1368. false,
  1369. input_file.is_in_sysroot(),
  1370. cmdline,
  1371. script_options,
  1372. &lex,
  1373. false,
  1374. NULL);
  1375. if (yyparse(&closure) != 0)
  1376. {
  1377. input_file.file().unlock(task);
  1378. return false;
  1379. }
  1380. input_file.file().unlock(task);
  1381. gold_assert(!closure.saw_inputs());
  1382. return true;
  1383. }
  1384. // FILENAME was found as an argument to --script (-T).
  1385. // Read it as a script, and execute its contents immediately.
  1386. bool
  1387. read_commandline_script(const char* filename, Command_line* cmdline)
  1388. {
  1389. return read_script_file(filename, cmdline, &cmdline->script_options(),
  1390. PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
  1391. }
  1392. // FILENAME was found as an argument to --version-script. Read it as
  1393. // a version script, and store its contents in
  1394. // cmdline->script_options()->version_script_info().
  1395. bool
  1396. read_version_script(const char* filename, Command_line* cmdline)
  1397. {
  1398. return read_script_file(filename, cmdline, &cmdline->script_options(),
  1399. PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
  1400. }
  1401. // FILENAME was found as an argument to --dynamic-list. Read it as a
  1402. // list of symbols, and store its contents in DYNAMIC_LIST.
  1403. bool
  1404. read_dynamic_list(const char* filename, Command_line* cmdline,
  1405. Script_options* dynamic_list)
  1406. {
  1407. return read_script_file(filename, cmdline, dynamic_list,
  1408. PARSING_DYNAMIC_LIST, Lex::DYNAMIC_LIST);
  1409. }
  1410. // Implement the --defsym option on the command line. Return true if
  1411. // all is well.
  1412. bool
  1413. Script_options::define_symbol(const char* definition)
  1414. {
  1415. Lex lex(definition, strlen(definition), PARSING_DEFSYM);
  1416. lex.set_mode(Lex::EXPRESSION);
  1417. // Dummy value.
  1418. Position_dependent_options posdep_options;
  1419. Parser_closure closure("command line", posdep_options, true,
  1420. false, false, NULL, this, &lex, false, NULL);
  1421. if (yyparse(&closure) != 0)
  1422. return false;
  1423. gold_assert(!closure.saw_inputs());
  1424. return true;
  1425. }
  1426. // Print the script to F for debugging.
  1427. void
  1428. Script_options::print(FILE* f) const
  1429. {
  1430. fprintf(f, "%s: Dumping linker script\n", program_name);
  1431. if (!this->entry_.empty())
  1432. fprintf(f, "ENTRY(%s)\n", this->entry_.c_str());
  1433. for (Symbol_assignments::const_iterator p =
  1434. this->symbol_assignments_.begin();
  1435. p != this->symbol_assignments_.end();
  1436. ++p)
  1437. (*p)->print(f);
  1438. for (Assertions::const_iterator p = this->assertions_.begin();
  1439. p != this->assertions_.end();
  1440. ++p)
  1441. (*p)->print(f);
  1442. this->script_sections_.print(f);
  1443. this->version_script_info_.print(f);
  1444. }
  1445. // Manage mapping from keywords to the codes expected by the bison
  1446. // parser. We construct one global object for each lex mode with
  1447. // keywords.
  1448. class Keyword_to_parsecode
  1449. {
  1450. public:
  1451. // The structure which maps keywords to parsecodes.
  1452. struct Keyword_parsecode
  1453. {
  1454. // Keyword.
  1455. const char* keyword;
  1456. // Corresponding parsecode.
  1457. int parsecode;
  1458. };
  1459. Keyword_to_parsecode(const Keyword_parsecode* keywords,
  1460. int keyword_count)
  1461. : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
  1462. { }
  1463. // Return the parsecode corresponding KEYWORD, or 0 if it is not a
  1464. // keyword.
  1465. int
  1466. keyword_to_parsecode(const char* keyword, size_t len) const;
  1467. private:
  1468. const Keyword_parsecode* keyword_parsecodes_;
  1469. const int keyword_count_;
  1470. };
  1471. // Mapping from keyword string to keyword parsecode. This array must
  1472. // be kept in sorted order. Parsecodes are looked up using bsearch.
  1473. // This array must correspond to the list of parsecodes in yyscript.y.
  1474. static const Keyword_to_parsecode::Keyword_parsecode
  1475. script_keyword_parsecodes[] =
  1476. {
  1477. { "ABSOLUTE", ABSOLUTE },
  1478. { "ADDR", ADDR },
  1479. { "ALIGN", ALIGN_K },
  1480. { "ALIGNOF", ALIGNOF },
  1481. { "ASSERT", ASSERT_K },
  1482. { "AS_NEEDED", AS_NEEDED },
  1483. { "AT", AT },
  1484. { "BIND", BIND },
  1485. { "BLOCK", BLOCK },
  1486. { "BYTE", BYTE },
  1487. { "CONSTANT", CONSTANT },
  1488. { "CONSTRUCTORS", CONSTRUCTORS },
  1489. { "COPY", COPY },
  1490. { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
  1491. { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
  1492. { "DATA_SEGMENT_END", DATA_SEGMENT_END },
  1493. { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
  1494. { "DEFINED", DEFINED },
  1495. { "DSECT", DSECT },
  1496. { "ENTRY", ENTRY },
  1497. { "EXCLUDE_FILE", EXCLUDE_FILE },
  1498. { "EXTERN", EXTERN },
  1499. { "FILL", FILL },
  1500. { "FLOAT", FLOAT },
  1501. { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
  1502. { "GROUP", GROUP },
  1503. { "HLL", HLL },
  1504. { "INCLUDE", INCLUDE },
  1505. { "INFO", INFO },
  1506. { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
  1507. { "INPUT", INPUT },
  1508. { "KEEP", KEEP },
  1509. { "LENGTH", LENGTH },
  1510. { "LOADADDR", LOADADDR },
  1511. { "LONG", LONG },
  1512. { "MAP", MAP },
  1513. { "MAX", MAX_K },
  1514. { "MEMORY", MEMORY },
  1515. { "MIN", MIN_K },
  1516. { "NEXT", NEXT },
  1517. { "NOCROSSREFS", NOCROSSREFS },
  1518. { "NOFLOAT", NOFLOAT },
  1519. { "NOLOAD", NOLOAD },
  1520. { "ONLY_IF_RO", ONLY_IF_RO },
  1521. { "ONLY_IF_RW", ONLY_IF_RW },
  1522. { "OPTION", OPTION },
  1523. { "ORIGIN", ORIGIN },
  1524. { "OUTPUT", OUTPUT },
  1525. { "OUTPUT_ARCH", OUTPUT_ARCH },
  1526. { "OUTPUT_FORMAT", OUTPUT_FORMAT },
  1527. { "OVERLAY", OVERLAY },
  1528. { "PHDRS", PHDRS },
  1529. { "PROVIDE", PROVIDE },
  1530. { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
  1531. { "QUAD", QUAD },
  1532. { "SEARCH_DIR", SEARCH_DIR },
  1533. { "SECTIONS", SECTIONS },
  1534. { "SEGMENT_START", SEGMENT_START },
  1535. { "SHORT", SHORT },
  1536. { "SIZEOF", SIZEOF },
  1537. { "SIZEOF_HEADERS", SIZEOF_HEADERS },
  1538. { "SORT", SORT_BY_NAME },
  1539. { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
  1540. { "SORT_BY_NAME", SORT_BY_NAME },
  1541. { "SPECIAL", SPECIAL },
  1542. { "SQUAD", SQUAD },
  1543. { "STARTUP", STARTUP },
  1544. { "SUBALIGN", SUBALIGN },
  1545. { "SYSLIB", SYSLIB },
  1546. { "TARGET", TARGET_K },
  1547. { "TRUNCATE", TRUNCATE },
  1548. { "VERSION", VERSIONK },
  1549. { "global", GLOBAL },
  1550. { "l", LENGTH },
  1551. { "len", LENGTH },
  1552. { "local", LOCAL },
  1553. { "o", ORIGIN },
  1554. { "org", ORIGIN },
  1555. { "sizeof_headers", SIZEOF_HEADERS },
  1556. };
  1557. static const Keyword_to_parsecode
  1558. script_keywords(&script_keyword_parsecodes[0],
  1559. (sizeof(script_keyword_parsecodes)
  1560. / sizeof(script_keyword_parsecodes[0])));
  1561. static const Keyword_to_parsecode::Keyword_parsecode
  1562. version_script_keyword_parsecodes[] =
  1563. {
  1564. { "extern", EXTERN },
  1565. { "global", GLOBAL },
  1566. { "local", LOCAL },
  1567. };
  1568. static const Keyword_to_parsecode
  1569. version_script_keywords(&version_script_keyword_parsecodes[0],
  1570. (sizeof(version_script_keyword_parsecodes)
  1571. / sizeof(version_script_keyword_parsecodes[0])));
  1572. static const Keyword_to_parsecode::Keyword_parsecode
  1573. dynamic_list_keyword_parsecodes[] =
  1574. {
  1575. { "extern", EXTERN },
  1576. };
  1577. static const Keyword_to_parsecode
  1578. dynamic_list_keywords(&dynamic_list_keyword_parsecodes[0],
  1579. (sizeof(dynamic_list_keyword_parsecodes)
  1580. / sizeof(dynamic_list_keyword_parsecodes[0])));
  1581. // Comparison function passed to bsearch.
  1582. extern "C"
  1583. {
  1584. struct Ktt_key
  1585. {
  1586. const char* str;
  1587. size_t len;
  1588. };
  1589. static int
  1590. ktt_compare(const void* keyv, const void* kttv)
  1591. {
  1592. const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
  1593. const Keyword_to_parsecode::Keyword_parsecode* ktt =
  1594. static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
  1595. int i = strncmp(key->str, ktt->keyword, key->len);
  1596. if (i != 0)
  1597. return i;
  1598. if (ktt->keyword[key->len] != '\0')
  1599. return -1;
  1600. return 0;
  1601. }
  1602. } // End extern "C".
  1603. int
  1604. Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
  1605. size_t len) const
  1606. {
  1607. Ktt_key key;
  1608. key.str = keyword;
  1609. key.len = len;
  1610. void* kttv = bsearch(&key,
  1611. this->keyword_parsecodes_,
  1612. this->keyword_count_,
  1613. sizeof(this->keyword_parsecodes_[0]),
  1614. ktt_compare);
  1615. if (kttv == NULL)
  1616. return 0;
  1617. Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
  1618. return ktt->parsecode;
  1619. }
  1620. // The following structs are used within the VersionInfo class as well
  1621. // as in the bison helper functions. They store the information
  1622. // parsed from the version script.
  1623. // A single version expression.
  1624. // For example, pattern="std::map*" and language="C++".
  1625. struct Version_expression
  1626. {
  1627. Version_expression(const std::string& a_pattern,
  1628. Version_script_info::Language a_language,
  1629. bool a_exact_match)
  1630. : pattern(a_pattern), language(a_language), exact_match(a_exact_match),
  1631. was_matched_by_symbol(false)
  1632. { }
  1633. std::string pattern;
  1634. Version_script_info::Language language;
  1635. // If false, we use glob() to match pattern. If true, we use strcmp().
  1636. bool exact_match;
  1637. // True if --no-undefined-version is in effect and we found this
  1638. // version in get_symbol_version. We use mutable because this
  1639. // struct is generally not modifiable after it has been created.
  1640. mutable bool was_matched_by_symbol;
  1641. };
  1642. // A list of expressions.
  1643. struct Version_expression_list
  1644. {
  1645. std::vector<struct Version_expression> expressions;
  1646. };
  1647. // A list of which versions upon which another version depends.
  1648. // Strings should be from the Stringpool.
  1649. struct Version_dependency_list
  1650. {
  1651. std::vector<std::string> dependencies;
  1652. };
  1653. // The total definition of a version. It includes the tag for the
  1654. // version, its global and local expressions, and any dependencies.
  1655. struct Version_tree
  1656. {
  1657. Version_tree()
  1658. : tag(), global(NULL), local(NULL), dependencies(NULL)
  1659. { }
  1660. std::string tag;
  1661. const struct Version_expression_list* global;
  1662. const struct Version_expression_list* local;
  1663. const struct Version_dependency_list* dependencies;
  1664. };
  1665. // Helper class that calls cplus_demangle when needed and takes care of freeing
  1666. // the result.
  1667. class Lazy_demangler
  1668. {
  1669. public:
  1670. Lazy_demangler(const char* symbol, int options)
  1671. : symbol_(symbol), options_(options), demangled_(NULL), did_demangle_(false)
  1672. { }
  1673. ~Lazy_demangler()
  1674. { free(this->demangled_); }
  1675. // Return the demangled name. The actual demangling happens on the first call,
  1676. // and the result is later cached.
  1677. inline char*
  1678. get();
  1679. private:
  1680. // The symbol to demangle.
  1681. const char* symbol_;
  1682. // Option flags to pass to cplus_demagle.
  1683. const int options_;
  1684. // The cached demangled value, or NULL if demangling didn't happen yet or
  1685. // failed.
  1686. char* demangled_;
  1687. // Whether we already called cplus_demangle
  1688. bool did_demangle_;
  1689. };
  1690. // Return the demangled name. The actual demangling happens on the first call,
  1691. // and the result is later cached. Returns NULL if the symbol cannot be
  1692. // demangled.
  1693. inline char*
  1694. Lazy_demangler::get()
  1695. {
  1696. if (!this->did_demangle_)
  1697. {
  1698. this->demangled_ = cplus_demangle(this->symbol_, this->options_);
  1699. this->did_demangle_ = true;
  1700. }
  1701. return this->demangled_;
  1702. }
  1703. // Class Version_script_info.
  1704. Version_script_info::Version_script_info()
  1705. : dependency_lists_(), expression_lists_(), version_trees_(), globs_(),
  1706. default_version_(NULL), default_is_global_(false), is_finalized_(false)
  1707. {
  1708. for (int i = 0; i < LANGUAGE_COUNT; ++i)
  1709. this->exact_[i] = NULL;
  1710. }
  1711. Version_script_info::~Version_script_info()
  1712. {
  1713. }
  1714. // Forget all the known version script information.
  1715. void
  1716. Version_script_info::clear()
  1717. {
  1718. for (size_t k = 0; k < this->dependency_lists_.size(); ++k)
  1719. delete this->dependency_lists_[k];
  1720. this->dependency_lists_.clear();
  1721. for (size_t k = 0; k < this->version_trees_.size(); ++k)
  1722. delete this->version_trees_[k];
  1723. this->version_trees_.clear();
  1724. for (size_t k = 0; k < this->expression_lists_.size(); ++k)
  1725. delete this->expression_lists_[k];
  1726. this->expression_lists_.clear();
  1727. }
  1728. // Finalize the version script information.
  1729. void
  1730. Version_script_info::finalize()
  1731. {
  1732. if (!this->is_finalized_)
  1733. {
  1734. this->build_lookup_tables();
  1735. this->is_finalized_ = true;
  1736. }
  1737. }
  1738. // Return all the versions.
  1739. std::vector<std::string>
  1740. Version_script_info::get_versions() const
  1741. {
  1742. std::vector<std::string> ret;
  1743. for (size_t j = 0; j < this->version_trees_.size(); ++j)
  1744. if (!this->version_trees_[j]->tag.empty())
  1745. ret.push_back(this->version_trees_[j]->tag);
  1746. return ret;
  1747. }
  1748. // Return the dependencies of VERSION.
  1749. std::vector<std::string>
  1750. Version_script_info::get_dependencies(const char* version) const
  1751. {
  1752. std::vector<std::string> ret;
  1753. for (size_t j = 0; j < this->version_trees_.size(); ++j)
  1754. if (this->version_trees_[j]->tag == version)
  1755. {
  1756. const struct Version_dependency_list* deps =
  1757. this->version_trees_[j]->dependencies;
  1758. if (deps != NULL)
  1759. for (size_t k = 0; k < deps->dependencies.size(); ++k)
  1760. ret.push_back(deps->dependencies[k]);
  1761. return ret;
  1762. }
  1763. return ret;
  1764. }
  1765. // A version script essentially maps a symbol name to a version tag
  1766. // and an indication of whether symbol is global or local within that
  1767. // version tag. Each symbol maps to at most one version tag.
  1768. // Unfortunately, in practice, version scripts are ambiguous, and list
  1769. // symbols multiple times. Thus, we have to document the matching
  1770. // process.
  1771. // This is a description of what the GNU linker does as of 2010-01-11.
  1772. // It walks through the version tags in the order in which they appear
  1773. // in the version script. For each tag, it first walks through the
  1774. // global patterns for that tag, then the local patterns. When
  1775. // looking at a single pattern, it first applies any language specific
  1776. // demangling as specified for the pattern, and then matches the
  1777. // resulting symbol name to the pattern. If it finds an exact match
  1778. // for a literal pattern (a pattern enclosed in quotes or with no
  1779. // wildcard characters), then that is the match that it uses. If
  1780. // finds a match with a wildcard pattern, then it saves it and
  1781. // continues searching. Wildcard patterns that are exactly "*" are
  1782. // saved separately.
  1783. // If no exact match with a literal pattern is ever found, then if a
  1784. // wildcard match with a global pattern was found it is used,
  1785. // otherwise if a wildcard match with a local pattern was found it is
  1786. // used.
  1787. // This is the result:
  1788. // * If there is an exact match, then we use the first tag in the
  1789. // version script where it matches.
  1790. // + If the exact match in that tag is global, it is used.
  1791. // + Otherwise the exact match in that tag is local, and is used.
  1792. // * Otherwise, if there is any match with a global wildcard pattern:
  1793. // + If there is any match with a wildcard pattern which is not
  1794. // "*", then we use the tag in which the *last* such pattern
  1795. // appears.
  1796. // + Otherwise, we matched "*". If there is no match with a local
  1797. // wildcard pattern which is not "*", then we use the *last*
  1798. // match with a global "*". Otherwise, continue.
  1799. // * Otherwise, if there is any match with a local wildcard pattern:
  1800. // + If there is any match with a wildcard pattern which is not
  1801. // "*", then we use the tag in which the *last* such pattern
  1802. // appears.
  1803. // + Otherwise, we matched "*", and we use the tag in which the
  1804. // *last* such match occurred.
  1805. // There is an additional wrinkle. When the GNU linker finds a symbol
  1806. // with a version defined in an object file due to a .symver
  1807. // directive, it looks up that symbol name in that version tag. If it
  1808. // finds it, it matches the symbol name against the patterns for that
  1809. // version. If there is no match with a global pattern, but there is
  1810. // a match with a local pattern, then the GNU linker marks the symbol
  1811. // as local.
  1812. // We want gold to be generally compatible, but we also want gold to
  1813. // be fast. These are the rules that gold implements:
  1814. // * If there is an exact match for the mangled name, we use it.
  1815. // + If there is more than one exact match, we give a warning, and
  1816. // we use the first tag in the script which matches.
  1817. // + If a symbol has an exact match as both global and local for
  1818. // the same version tag, we give an error.
  1819. // * Otherwise, we look for an extern C++ or an extern Java exact
  1820. // match. If we find an exact match, we use it.
  1821. // + If there is more than one exact match, we give a warning, and
  1822. // we use the first tag in the script which matches.
  1823. // + If a symbol has an exact match as both global and local for
  1824. // the same version tag, we give an error.
  1825. // * Otherwise, we look through the wildcard patterns, ignoring "*"
  1826. // patterns. We look through the version tags in reverse order.
  1827. // For each version tag, we look through the global patterns and
  1828. // then the local patterns. We use the first match we find (i.e.,
  1829. // the last matching version tag in the file).
  1830. // * Otherwise, we use the "*" pattern if there is one. We give an
  1831. // error if there are multiple "*" patterns.
  1832. // At least for now, gold does not look up the version tag for a
  1833. // symbol version found in an object file to see if it should be
  1834. // forced local. There are other ways to force a symbol to be local,
  1835. // and I don't understand why this one is useful.
  1836. // Build a set of fast lookup tables for a version script.
  1837. void
  1838. Version_script_info::build_lookup_tables()
  1839. {
  1840. size_t size = this->version_trees_.size();
  1841. for (size_t j = 0; j < size; ++j)
  1842. {
  1843. const Version_tree* v = this->version_trees_[j];
  1844. this->build_expression_list_lookup(v->local, v, false);
  1845. this->build_expression_list_lookup(v->global, v, true);
  1846. }
  1847. }
  1848. // If a pattern has backlashes but no unquoted wildcard characters,
  1849. // then we apply backslash unquoting and look for an exact match.
  1850. // Otherwise we treat it as a wildcard pattern. This function returns
  1851. // true for a wildcard pattern. Otherwise, it does backslash
  1852. // unquoting on *PATTERN and returns false. If this returns true,
  1853. // *PATTERN may have been partially unquoted.
  1854. bool
  1855. Version_script_info::unquote(std::string* pattern) const
  1856. {
  1857. bool saw_backslash = false;
  1858. size_t len = pattern->length();
  1859. size_t j = 0;
  1860. for (size_t i = 0; i < len; ++i)
  1861. {
  1862. if (saw_backslash)
  1863. saw_backslash = false;
  1864. else
  1865. {
  1866. switch ((*pattern)[i])
  1867. {
  1868. case '?': case '[': case '*':
  1869. return true;
  1870. case '\\':
  1871. saw_backslash = true;
  1872. continue;
  1873. default:
  1874. break;
  1875. }
  1876. }
  1877. if (i != j)
  1878. (*pattern)[j] = (*pattern)[i];
  1879. ++j;
  1880. }
  1881. return false;
  1882. }
  1883. // Add an exact match for MATCH to *PE. The result of the match is
  1884. // V/IS_GLOBAL.
  1885. void
  1886. Version_script_info::add_exact_match(const std::string& match,
  1887. const Version_tree* v, bool is_global,
  1888. const Version_expression* ve,
  1889. Exact* pe)
  1890. {
  1891. std::pair<Exact::iterator, bool> ins =
  1892. pe->insert(std::make_pair(match, Version_tree_match(v, is_global, ve)));
  1893. if (ins.second)
  1894. {
  1895. // This is the first time we have seen this match.
  1896. return;
  1897. }
  1898. Version_tree_match& vtm(ins.first->second);
  1899. if (vtm.real->tag != v->tag)
  1900. {
  1901. // This is an ambiguous match. We still return the
  1902. // first version that we found in the script, but we
  1903. // record the new version to issue a warning if we
  1904. // wind up looking up this symbol.
  1905. if (vtm.ambiguous == NULL)
  1906. vtm.ambiguous = v;
  1907. }
  1908. else if (is_global != vtm.is_global)
  1909. {
  1910. // We have a match for both the global and local entries for a
  1911. // version tag. That's got to be wrong.
  1912. gold_error(_("'%s' appears as both a global and a local symbol "
  1913. "for version '%s' in script"),
  1914. match.c_str(), v->tag.c_str());
  1915. }
  1916. }
  1917. // Build fast lookup information for EXPLIST and store it in LOOKUP.
  1918. // All matches go to V, and IS_GLOBAL is true if they are global
  1919. // matches.
  1920. void
  1921. Version_script_info::build_expression_list_lookup(
  1922. const Version_expression_list* explist,
  1923. const Version_tree* v,
  1924. bool is_global)
  1925. {
  1926. if (explist == NULL)
  1927. return;
  1928. size_t size = explist->expressions.size();
  1929. for (size_t i = 0; i < size; ++i)
  1930. {
  1931. const Version_expression& exp(explist->expressions[i]);
  1932. if (exp.pattern.length() == 1 && exp.pattern[0] == '*')
  1933. {
  1934. if (this->default_version_ != NULL
  1935. && this->default_version_->tag != v->tag)
  1936. gold_warning(_("wildcard match appears in both version '%s' "
  1937. "and '%s' in script"),
  1938. this->default_version_->tag.c_str(), v->tag.c_str());
  1939. else if (this->default_version_ != NULL
  1940. && this->default_is_global_ != is_global)
  1941. gold_error(_("wildcard match appears as both global and local "
  1942. "in version '%s' in script"),
  1943. v->tag.c_str());
  1944. this->default_version_ = v;
  1945. this->default_is_global_ = is_global;
  1946. continue;
  1947. }
  1948. std::string pattern = exp.pattern;
  1949. if (!exp.exact_match)
  1950. {
  1951. if (this->unquote(&pattern))
  1952. {
  1953. this->globs_.push_back(Glob(&exp, v, is_global));
  1954. continue;
  1955. }
  1956. }
  1957. if (this->exact_[exp.language] == NULL)
  1958. this->exact_[exp.language] = new Exact();
  1959. this->add_exact_match(pattern, v, is_global, &exp,
  1960. this->exact_[exp.language]);
  1961. }
  1962. }
  1963. // Return the name to match given a name, a language code, and two
  1964. // lazy demanglers.
  1965. const char*
  1966. Version_script_info::get_name_to_match(const char* name,
  1967. int language,
  1968. Lazy_demangler* cpp_demangler,
  1969. Lazy_demangler* java_demangler) const
  1970. {
  1971. switch (language)
  1972. {
  1973. case LANGUAGE_C:
  1974. return name;
  1975. case LANGUAGE_CXX:
  1976. return cpp_demangler->get();
  1977. case LANGUAGE_JAVA:
  1978. return java_demangler->get();
  1979. default:
  1980. gold_unreachable();
  1981. }
  1982. }
  1983. // Look up SYMBOL_NAME in the list of versions. Return true if the
  1984. // symbol is found, false if not. If the symbol is found, then if
  1985. // PVERSION is not NULL, set *PVERSION to the version tag, and if
  1986. // P_IS_GLOBAL is not NULL, set *P_IS_GLOBAL according to whether the
  1987. // symbol is global or not.
  1988. bool
  1989. Version_script_info::get_symbol_version(const char* symbol_name,
  1990. std::string* pversion,
  1991. bool* p_is_global) const
  1992. {
  1993. Lazy_demangler cpp_demangled_name(symbol_name, DMGL_ANSI | DMGL_PARAMS);
  1994. Lazy_demangler java_demangled_name(symbol_name,
  1995. DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
  1996. gold_assert(this->is_finalized_);
  1997. for (int i = 0; i < LANGUAGE_COUNT; ++i)
  1998. {
  1999. Exact* exact = this->exact_[i];
  2000. if (exact == NULL)
  2001. continue;
  2002. const char* name_to_match = this->get_name_to_match(symbol_name, i,
  2003. &cpp_demangled_name,
  2004. &java_demangled_name);
  2005. if (name_to_match == NULL)
  2006. {
  2007. // If the name can not be demangled, the GNU linker goes
  2008. // ahead and tries to match it anyhow. That does not
  2009. // make sense to me and I have not implemented it.
  2010. continue;
  2011. }
  2012. Exact::const_iterator pe = exact->find(name_to_match);
  2013. if (pe != exact->end())
  2014. {
  2015. const Version_tree_match& vtm(pe->second);
  2016. if (vtm.ambiguous != NULL)
  2017. gold_warning(_("using '%s' as version for '%s' which is also "
  2018. "named in version '%s' in script"),
  2019. vtm.real->tag.c_str(), name_to_match,
  2020. vtm.ambiguous->tag.c_str());
  2021. if (pversion != NULL)
  2022. *pversion = vtm.real->tag;
  2023. if (p_is_global != NULL)
  2024. *p_is_global = vtm.is_global;
  2025. // If we are using --no-undefined-version, and this is a
  2026. // global symbol, we have to record that we have found this
  2027. // symbol, so that we don't warn about it. We have to do
  2028. // this now, because otherwise we have no way to get from a
  2029. // non-C language back to the demangled name that we
  2030. // matched.
  2031. if (p_is_global != NULL && vtm.is_global)
  2032. vtm.expression->was_matched_by_symbol = true;
  2033. return true;
  2034. }
  2035. }
  2036. // Look through the glob patterns in reverse order.
  2037. for (Globs::const_reverse_iterator p = this->globs_.rbegin();
  2038. p != this->globs_.rend();
  2039. ++p)
  2040. {
  2041. int language = p->expression->language;
  2042. const char* name_to_match = this->get_name_to_match(symbol_name,
  2043. language,
  2044. &cpp_demangled_name,
  2045. &java_demangled_name);
  2046. if (name_to_match == NULL)
  2047. continue;
  2048. if (fnmatch(p->expression->pattern.c_str(), name_to_match,
  2049. FNM_NOESCAPE) == 0)
  2050. {
  2051. if (pversion != NULL)
  2052. *pversion = p->version->tag;
  2053. if (p_is_global != NULL)
  2054. *p_is_global = p->is_global;
  2055. return true;
  2056. }
  2057. }
  2058. // Finally, there may be a wildcard.
  2059. if (this->default_version_ != NULL)
  2060. {
  2061. if (pversion != NULL)
  2062. *pversion = this->default_version_->tag;
  2063. if (p_is_global != NULL)
  2064. *p_is_global = this->default_is_global_;
  2065. return true;
  2066. }
  2067. return false;
  2068. }
  2069. // Give an error if any exact symbol names (not wildcards) appear in a
  2070. // version script, but there is no such symbol.
  2071. void
  2072. Version_script_info::check_unmatched_names(const Symbol_table* symtab) const
  2073. {
  2074. for (size_t i = 0; i < this->version_trees_.size(); ++i)
  2075. {
  2076. const Version_tree* vt = this->version_trees_[i];
  2077. if (vt->global == NULL)
  2078. continue;
  2079. for (size_t j = 0; j < vt->global->expressions.size(); ++j)
  2080. {
  2081. const Version_expression& expression(vt->global->expressions[j]);
  2082. // Ignore cases where we used the version because we saw a
  2083. // symbol that we looked up. Note that
  2084. // WAS_MATCHED_BY_SYMBOL will be true even if the symbol was
  2085. // not a definition. That's OK as in that case we most
  2086. // likely gave an undefined symbol error anyhow.
  2087. if (expression.was_matched_by_symbol)
  2088. continue;
  2089. // Just ignore names which are in languages other than C.
  2090. // We have no way to look them up in the symbol table.
  2091. if (expression.language != LANGUAGE_C)
  2092. continue;
  2093. // Remove backslash quoting, and ignore wildcard patterns.
  2094. std::string pattern = expression.pattern;
  2095. if (!expression.exact_match)
  2096. {
  2097. if (this->unquote(&pattern))
  2098. continue;
  2099. }
  2100. if (symtab->lookup(pattern.c_str(), vt->tag.c_str()) == NULL)
  2101. gold_error(_("version script assignment of %s to symbol %s "
  2102. "failed: symbol not defined"),
  2103. vt->tag.c_str(), pattern.c_str());
  2104. }
  2105. }
  2106. }
  2107. struct Version_dependency_list*
  2108. Version_script_info::allocate_dependency_list()
  2109. {
  2110. dependency_lists_.push_back(new Version_dependency_list);
  2111. return dependency_lists_.back();
  2112. }
  2113. struct Version_expression_list*
  2114. Version_script_info::allocate_expression_list()
  2115. {
  2116. expression_lists_.push_back(new Version_expression_list);
  2117. return expression_lists_.back();
  2118. }
  2119. struct Version_tree*
  2120. Version_script_info::allocate_version_tree()
  2121. {
  2122. version_trees_.push_back(new Version_tree);
  2123. return version_trees_.back();
  2124. }
  2125. // Print for debugging.
  2126. void
  2127. Version_script_info::print(FILE* f) const
  2128. {
  2129. if (this->empty())
  2130. return;
  2131. fprintf(f, "VERSION {");
  2132. for (size_t i = 0; i < this->version_trees_.size(); ++i)
  2133. {
  2134. const Version_tree* vt = this->version_trees_[i];
  2135. if (vt->tag.empty())
  2136. fprintf(f, " {\n");
  2137. else
  2138. fprintf(f, " %s {\n", vt->tag.c_str());
  2139. if (vt->global != NULL)
  2140. {
  2141. fprintf(f, " global :\n");
  2142. this->print_expression_list(f, vt->global);
  2143. }
  2144. if (vt->local != NULL)
  2145. {
  2146. fprintf(f, " local :\n");
  2147. this->print_expression_list(f, vt->local);
  2148. }
  2149. fprintf(f, " }");
  2150. if (vt->dependencies != NULL)
  2151. {
  2152. const Version_dependency_list* deps = vt->dependencies;
  2153. for (size_t j = 0; j < deps->dependencies.size(); ++j)
  2154. {
  2155. if (j < deps->dependencies.size() - 1)
  2156. fprintf(f, "\n");
  2157. fprintf(f, " %s", deps->dependencies[j].c_str());
  2158. }
  2159. }
  2160. fprintf(f, ";\n");
  2161. }
  2162. fprintf(f, "}\n");
  2163. }
  2164. void
  2165. Version_script_info::print_expression_list(
  2166. FILE* f,
  2167. const Version_expression_list* vel) const
  2168. {
  2169. Version_script_info::Language current_language = LANGUAGE_C;
  2170. for (size_t i = 0; i < vel->expressions.size(); ++i)
  2171. {
  2172. const Version_expression& ve(vel->expressions[i]);
  2173. if (ve.language != current_language)
  2174. {
  2175. if (current_language != LANGUAGE_C)
  2176. fprintf(f, " }\n");
  2177. switch (ve.language)
  2178. {
  2179. case LANGUAGE_C:
  2180. break;
  2181. case LANGUAGE_CXX:
  2182. fprintf(f, " extern \"C++\" {\n");
  2183. break;
  2184. case LANGUAGE_JAVA:
  2185. fprintf(f, " extern \"Java\" {\n");
  2186. break;
  2187. default:
  2188. gold_unreachable();
  2189. }
  2190. current_language = ve.language;
  2191. }
  2192. fprintf(f, " ");
  2193. if (current_language != LANGUAGE_C)
  2194. fprintf(f, " ");
  2195. if (ve.exact_match)
  2196. fprintf(f, "\"");
  2197. fprintf(f, "%s", ve.pattern.c_str());
  2198. if (ve.exact_match)
  2199. fprintf(f, "\"");
  2200. fprintf(f, "\n");
  2201. }
  2202. if (current_language != LANGUAGE_C)
  2203. fprintf(f, " }\n");
  2204. }
  2205. } // End namespace gold.
  2206. // The remaining functions are extern "C", so it's clearer to not put
  2207. // them in namespace gold.
  2208. using namespace gold;
  2209. // This function is called by the bison parser to return the next
  2210. // token.
  2211. extern "C" int
  2212. yylex(YYSTYPE* lvalp, void* closurev)
  2213. {
  2214. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2215. const Token* token = closure->next_token();
  2216. switch (token->classification())
  2217. {
  2218. default:
  2219. gold_unreachable();
  2220. case Token::TOKEN_INVALID:
  2221. yyerror(closurev, "invalid character");
  2222. return 0;
  2223. case Token::TOKEN_EOF:
  2224. return 0;
  2225. case Token::TOKEN_STRING:
  2226. {
  2227. // This is either a keyword or a STRING.
  2228. size_t len;
  2229. const char* str = token->string_value(&len);
  2230. int parsecode = 0;
  2231. switch (closure->lex_mode())
  2232. {
  2233. case Lex::LINKER_SCRIPT:
  2234. parsecode = script_keywords.keyword_to_parsecode(str, len);
  2235. break;
  2236. case Lex::VERSION_SCRIPT:
  2237. parsecode = version_script_keywords.keyword_to_parsecode(str, len);
  2238. break;
  2239. case Lex::DYNAMIC_LIST:
  2240. parsecode = dynamic_list_keywords.keyword_to_parsecode(str, len);
  2241. break;
  2242. default:
  2243. break;
  2244. }
  2245. if (parsecode != 0)
  2246. return parsecode;
  2247. lvalp->string.value = str;
  2248. lvalp->string.length = len;
  2249. return STRING;
  2250. }
  2251. case Token::TOKEN_QUOTED_STRING:
  2252. lvalp->string.value = token->string_value(&lvalp->string.length);
  2253. return QUOTED_STRING;
  2254. case Token::TOKEN_OPERATOR:
  2255. return token->operator_value();
  2256. case Token::TOKEN_INTEGER:
  2257. lvalp->integer = token->integer_value();
  2258. return INTEGER;
  2259. }
  2260. }
  2261. // This function is called by the bison parser to report an error.
  2262. extern "C" void
  2263. yyerror(void* closurev, const char* message)
  2264. {
  2265. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2266. gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
  2267. closure->charpos(), message);
  2268. }
  2269. // Called by the bison parser to add an external symbol to the link.
  2270. extern "C" void
  2271. script_add_extern(void* closurev, const char* name, size_t length)
  2272. {
  2273. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2274. closure->script_options()->add_symbol_reference(name, length);
  2275. }
  2276. // Called by the bison parser to add a file to the link.
  2277. extern "C" void
  2278. script_add_file(void* closurev, const char* name, size_t length)
  2279. {
  2280. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2281. // If this is an absolute path, and we found the script in the
  2282. // sysroot, then we want to prepend the sysroot to the file name.
  2283. // For example, this is how we handle a cross link to the x86_64
  2284. // libc.so, which refers to /lib/libc.so.6.
  2285. std::string name_string(name, length);
  2286. const char* extra_search_path = ".";
  2287. std::string script_directory;
  2288. if (IS_ABSOLUTE_PATH(name_string.c_str()))
  2289. {
  2290. if (closure->is_in_sysroot())
  2291. {
  2292. const std::string& sysroot(parameters->options().sysroot());
  2293. gold_assert(!sysroot.empty());
  2294. name_string = sysroot + name_string;
  2295. }
  2296. }
  2297. else
  2298. {
  2299. // In addition to checking the normal library search path, we
  2300. // also want to check in the script-directory.
  2301. const char* slash = strrchr(closure->filename(), '/');
  2302. if (slash != NULL)
  2303. {
  2304. script_directory.assign(closure->filename(),
  2305. slash - closure->filename() + 1);
  2306. extra_search_path = script_directory.c_str();
  2307. }
  2308. }
  2309. Input_file_argument file(name_string.c_str(),
  2310. Input_file_argument::INPUT_FILE_TYPE_FILE,
  2311. extra_search_path, false,
  2312. closure->position_dependent_options());
  2313. Input_argument& arg = closure->inputs()->add_file(file);
  2314. arg.set_script_info(closure->script_info());
  2315. }
  2316. // Called by the bison parser to add a library to the link.
  2317. extern "C" void
  2318. script_add_library(void* closurev, const char* name, size_t length)
  2319. {
  2320. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2321. std::string name_string(name, length);
  2322. if (name_string[0] != 'l')
  2323. gold_error(_("library name must be prefixed with -l"));
  2324. Input_file_argument file(name_string.c_str() + 1,
  2325. Input_file_argument::INPUT_FILE_TYPE_LIBRARY,
  2326. "", false,
  2327. closure->position_dependent_options());
  2328. Input_argument& arg = closure->inputs()->add_file(file);
  2329. arg.set_script_info(closure->script_info());
  2330. }
  2331. // Called by the bison parser to start a group. If we are already in
  2332. // a group, that means that this script was invoked within a
  2333. // --start-group --end-group sequence on the command line, or that
  2334. // this script was found in a GROUP of another script. In that case,
  2335. // we simply continue the existing group, rather than starting a new
  2336. // one. It is possible to construct a case in which this will do
  2337. // something other than what would happen if we did a recursive group,
  2338. // but it's hard to imagine why the different behaviour would be
  2339. // useful for a real program. Avoiding recursive groups is simpler
  2340. // and more efficient.
  2341. extern "C" void
  2342. script_start_group(void* closurev)
  2343. {
  2344. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2345. if (!closure->in_group())
  2346. closure->inputs()->start_group();
  2347. }
  2348. // Called by the bison parser at the end of a group.
  2349. extern "C" void
  2350. script_end_group(void* closurev)
  2351. {
  2352. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2353. if (!closure->in_group())
  2354. closure->inputs()->end_group();
  2355. }
  2356. // Called by the bison parser to start an AS_NEEDED list.
  2357. extern "C" void
  2358. script_start_as_needed(void* closurev)
  2359. {
  2360. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2361. closure->position_dependent_options().set_as_needed(true);
  2362. }
  2363. // Called by the bison parser at the end of an AS_NEEDED list.
  2364. extern "C" void
  2365. script_end_as_needed(void* closurev)
  2366. {
  2367. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2368. closure->position_dependent_options().set_as_needed(false);
  2369. }
  2370. // Called by the bison parser to set the entry symbol.
  2371. extern "C" void
  2372. script_set_entry(void* closurev, const char* entry, size_t length)
  2373. {
  2374. // We'll parse this exactly the same as --entry=ENTRY on the commandline
  2375. // TODO(csilvers): FIXME -- call set_entry directly.
  2376. std::string arg("--entry=");
  2377. arg.append(entry, length);
  2378. script_parse_option(closurev, arg.c_str(), arg.size());
  2379. }
  2380. // Called by the bison parser to set whether to define common symbols.
  2381. extern "C" void
  2382. script_set_common_allocation(void* closurev, int set)
  2383. {
  2384. const char* arg = set != 0 ? "--define-common" : "--no-define-common";
  2385. script_parse_option(closurev, arg, strlen(arg));
  2386. }
  2387. // Called by the bison parser to refer to a symbol.
  2388. extern "C" Expression*
  2389. script_symbol(void* closurev, const char* name, size_t length)
  2390. {
  2391. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2392. if (length != 1 || name[0] != '.')
  2393. closure->script_options()->add_symbol_reference(name, length);
  2394. return script_exp_string(name, length);
  2395. }
  2396. // Called by the bison parser to define a symbol.
  2397. extern "C" void
  2398. script_set_symbol(void* closurev, const char* name, size_t length,
  2399. Expression* value, int providei, int hiddeni)
  2400. {
  2401. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2402. const bool provide = providei != 0;
  2403. const bool hidden = hiddeni != 0;
  2404. closure->script_options()->add_symbol_assignment(name, length,
  2405. closure->parsing_defsym(),
  2406. value, provide, hidden);
  2407. closure->clear_skip_on_incompatible_target();
  2408. }
  2409. // Called by the bison parser to add an assertion.
  2410. extern "C" void
  2411. script_add_assertion(void* closurev, Expression* check, const char* message,
  2412. size_t messagelen)
  2413. {
  2414. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2415. closure->script_options()->add_assertion(check, message, messagelen);
  2416. closure->clear_skip_on_incompatible_target();
  2417. }
  2418. // Called by the bison parser to parse an OPTION.
  2419. extern "C" void
  2420. script_parse_option(void* closurev, const char* option, size_t length)
  2421. {
  2422. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2423. // We treat the option as a single command-line option, even if
  2424. // it has internal whitespace.
  2425. if (closure->command_line() == NULL)
  2426. {
  2427. // There are some options that we could handle here--e.g.,
  2428. // -lLIBRARY. Should we bother?
  2429. gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
  2430. " for scripts specified via -T/--script"),
  2431. closure->filename(), closure->lineno(), closure->charpos());
  2432. }
  2433. else
  2434. {
  2435. bool past_a_double_dash_option = false;
  2436. const char* mutable_option = strndup(option, length);
  2437. gold_assert(mutable_option != NULL);
  2438. closure->command_line()->process_one_option(1, &mutable_option, 0,
  2439. &past_a_double_dash_option);
  2440. // The General_options class will quite possibly store a pointer
  2441. // into mutable_option, so we can't free it. In cases the class
  2442. // does not store such a pointer, this is a memory leak. Alas. :(
  2443. }
  2444. closure->clear_skip_on_incompatible_target();
  2445. }
  2446. // Called by the bison parser to handle OUTPUT_FORMAT. OUTPUT_FORMAT
  2447. // takes either one or three arguments. In the three argument case,
  2448. // the format depends on the endianness option, which we don't
  2449. // currently support (FIXME). If we see an OUTPUT_FORMAT for the
  2450. // wrong format, then we want to search for a new file. Returning 0
  2451. // here will cause the parser to immediately abort.
  2452. extern "C" int
  2453. script_check_output_format(void* closurev,
  2454. const char* default_name, size_t default_length,
  2455. const char*, size_t, const char*, size_t)
  2456. {
  2457. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2458. std::string name(default_name, default_length);
  2459. Target* target = select_target_by_bfd_name(name.c_str());
  2460. if (target == NULL || !parameters->is_compatible_target(target))
  2461. {
  2462. if (closure->skip_on_incompatible_target())
  2463. {
  2464. closure->set_found_incompatible_target();
  2465. return 0;
  2466. }
  2467. // FIXME: Should we warn about the unknown target?
  2468. }
  2469. return 1;
  2470. }
  2471. // Called by the bison parser to handle TARGET.
  2472. extern "C" void
  2473. script_set_target(void* closurev, const char* target, size_t len)
  2474. {
  2475. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2476. std::string s(target, len);
  2477. General_options::Object_format format_enum;
  2478. format_enum = General_options::string_to_object_format(s.c_str());
  2479. closure->position_dependent_options().set_format_enum(format_enum);
  2480. }
  2481. // Called by the bison parser to handle SEARCH_DIR. This is handled
  2482. // exactly like a -L option.
  2483. extern "C" void
  2484. script_add_search_dir(void* closurev, const char* option, size_t length)
  2485. {
  2486. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2487. if (closure->command_line() == NULL)
  2488. gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
  2489. " for scripts specified via -T/--script"),
  2490. closure->filename(), closure->lineno(), closure->charpos());
  2491. else if (!closure->command_line()->options().nostdlib())
  2492. {
  2493. std::string s = "-L" + std::string(option, length);
  2494. script_parse_option(closurev, s.c_str(), s.size());
  2495. }
  2496. }
  2497. /* Called by the bison parser to push the lexer into expression
  2498. mode. */
  2499. extern "C" void
  2500. script_push_lex_into_expression_mode(void* closurev)
  2501. {
  2502. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2503. closure->push_lex_mode(Lex::EXPRESSION);
  2504. }
  2505. /* Called by the bison parser to push the lexer into version
  2506. mode. */
  2507. extern "C" void
  2508. script_push_lex_into_version_mode(void* closurev)
  2509. {
  2510. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2511. if (closure->version_script()->is_finalized())
  2512. gold_error(_("%s:%d:%d: invalid use of VERSION in input file"),
  2513. closure->filename(), closure->lineno(), closure->charpos());
  2514. closure->push_lex_mode(Lex::VERSION_SCRIPT);
  2515. }
  2516. /* Called by the bison parser to pop the lexer mode. */
  2517. extern "C" void
  2518. script_pop_lex_mode(void* closurev)
  2519. {
  2520. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2521. closure->pop_lex_mode();
  2522. }
  2523. // Register an entire version node. For example:
  2524. //
  2525. // GLIBC_2.1 {
  2526. // global: foo;
  2527. // } GLIBC_2.0;
  2528. //
  2529. // - tag is "GLIBC_2.1"
  2530. // - tree contains the information "global: foo"
  2531. // - deps contains "GLIBC_2.0"
  2532. extern "C" void
  2533. script_register_vers_node(void*,
  2534. const char* tag,
  2535. int taglen,
  2536. struct Version_tree* tree,
  2537. struct Version_dependency_list* deps)
  2538. {
  2539. gold_assert(tree != NULL);
  2540. tree->dependencies = deps;
  2541. if (tag != NULL)
  2542. tree->tag = std::string(tag, taglen);
  2543. }
  2544. // Add a dependencies to the list of existing dependencies, if any,
  2545. // and return the expanded list.
  2546. extern "C" struct Version_dependency_list*
  2547. script_add_vers_depend(void* closurev,
  2548. struct Version_dependency_list* all_deps,
  2549. const char* depend_to_add, int deplen)
  2550. {
  2551. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2552. if (all_deps == NULL)
  2553. all_deps = closure->version_script()->allocate_dependency_list();
  2554. all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
  2555. return all_deps;
  2556. }
  2557. // Add a pattern expression to an existing list of expressions, if any.
  2558. extern "C" struct Version_expression_list*
  2559. script_new_vers_pattern(void* closurev,
  2560. struct Version_expression_list* expressions,
  2561. const char* pattern, int patlen, int exact_match)
  2562. {
  2563. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2564. if (expressions == NULL)
  2565. expressions = closure->version_script()->allocate_expression_list();
  2566. expressions->expressions.push_back(
  2567. Version_expression(std::string(pattern, patlen),
  2568. closure->get_current_language(),
  2569. static_cast<bool>(exact_match)));
  2570. return expressions;
  2571. }
  2572. // Attaches b to the end of a, and clears b. So a = a + b and b = {}.
  2573. extern "C" struct Version_expression_list*
  2574. script_merge_expressions(struct Version_expression_list* a,
  2575. struct Version_expression_list* b)
  2576. {
  2577. a->expressions.insert(a->expressions.end(),
  2578. b->expressions.begin(), b->expressions.end());
  2579. // We could delete b and remove it from expressions_lists_, but
  2580. // that's a lot of work. This works just as well.
  2581. b->expressions.clear();
  2582. return a;
  2583. }
  2584. // Combine the global and local expressions into a a Version_tree.
  2585. extern "C" struct Version_tree*
  2586. script_new_vers_node(void* closurev,
  2587. struct Version_expression_list* global,
  2588. struct Version_expression_list* local)
  2589. {
  2590. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2591. Version_tree* tree = closure->version_script()->allocate_version_tree();
  2592. tree->global = global;
  2593. tree->local = local;
  2594. return tree;
  2595. }
  2596. // Handle a transition in language, such as at the
  2597. // start or end of 'extern "C++"'
  2598. extern "C" void
  2599. version_script_push_lang(void* closurev, const char* lang, int langlen)
  2600. {
  2601. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2602. std::string language(lang, langlen);
  2603. Version_script_info::Language code;
  2604. if (language.empty() || language == "C")
  2605. code = Version_script_info::LANGUAGE_C;
  2606. else if (language == "C++")
  2607. code = Version_script_info::LANGUAGE_CXX;
  2608. else if (language == "Java")
  2609. code = Version_script_info::LANGUAGE_JAVA;
  2610. else
  2611. {
  2612. char* buf = new char[langlen + 100];
  2613. snprintf(buf, langlen + 100,
  2614. _("unrecognized version script language '%s'"),
  2615. language.c_str());
  2616. yyerror(closurev, buf);
  2617. delete[] buf;
  2618. code = Version_script_info::LANGUAGE_C;
  2619. }
  2620. closure->push_language(code);
  2621. }
  2622. extern "C" void
  2623. version_script_pop_lang(void* closurev)
  2624. {
  2625. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2626. closure->pop_language();
  2627. }
  2628. // Called by the bison parser to start a SECTIONS clause.
  2629. extern "C" void
  2630. script_start_sections(void* closurev)
  2631. {
  2632. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2633. closure->script_options()->script_sections()->start_sections();
  2634. closure->clear_skip_on_incompatible_target();
  2635. }
  2636. // Called by the bison parser to finish a SECTIONS clause.
  2637. extern "C" void
  2638. script_finish_sections(void* closurev)
  2639. {
  2640. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2641. closure->script_options()->script_sections()->finish_sections();
  2642. }
  2643. // Start processing entries for an output section.
  2644. extern "C" void
  2645. script_start_output_section(void* closurev, const char* name, size_t namelen,
  2646. const struct Parser_output_section_header* header)
  2647. {
  2648. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2649. closure->script_options()->script_sections()->start_output_section(name,
  2650. namelen,
  2651. header);
  2652. }
  2653. // Finish processing entries for an output section.
  2654. extern "C" void
  2655. script_finish_output_section(void* closurev,
  2656. const struct Parser_output_section_trailer* trail)
  2657. {
  2658. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2659. closure->script_options()->script_sections()->finish_output_section(trail);
  2660. }
  2661. // Add a data item (e.g., "WORD (0)") to the current output section.
  2662. extern "C" void
  2663. script_add_data(void* closurev, int data_token, Expression* val)
  2664. {
  2665. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2666. int size;
  2667. bool is_signed = true;
  2668. switch (data_token)
  2669. {
  2670. case QUAD:
  2671. size = 8;
  2672. is_signed = false;
  2673. break;
  2674. case SQUAD:
  2675. size = 8;
  2676. break;
  2677. case LONG:
  2678. size = 4;
  2679. break;
  2680. case SHORT:
  2681. size = 2;
  2682. break;
  2683. case BYTE:
  2684. size = 1;
  2685. break;
  2686. default:
  2687. gold_unreachable();
  2688. }
  2689. closure->script_options()->script_sections()->add_data(size, is_signed, val);
  2690. }
  2691. // Add a clause setting the fill value to the current output section.
  2692. extern "C" void
  2693. script_add_fill(void* closurev, Expression* val)
  2694. {
  2695. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2696. closure->script_options()->script_sections()->add_fill(val);
  2697. }
  2698. // Add a new input section specification to the current output
  2699. // section.
  2700. extern "C" void
  2701. script_add_input_section(void* closurev,
  2702. const struct Input_section_spec* spec,
  2703. int keepi)
  2704. {
  2705. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2706. bool keep = keepi != 0;
  2707. closure->script_options()->script_sections()->add_input_section(spec, keep);
  2708. }
  2709. // When we see DATA_SEGMENT_ALIGN we record that following output
  2710. // sections may be relro.
  2711. extern "C" void
  2712. script_data_segment_align(void* closurev)
  2713. {
  2714. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2715. if (!closure->script_options()->saw_sections_clause())
  2716. gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
  2717. closure->filename(), closure->lineno(), closure->charpos());
  2718. else
  2719. closure->script_options()->script_sections()->data_segment_align();
  2720. }
  2721. // When we see DATA_SEGMENT_RELRO_END we know that all output sections
  2722. // since DATA_SEGMENT_ALIGN should be relro.
  2723. extern "C" void
  2724. script_data_segment_relro_end(void* closurev)
  2725. {
  2726. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2727. if (!closure->script_options()->saw_sections_clause())
  2728. gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
  2729. closure->filename(), closure->lineno(), closure->charpos());
  2730. else
  2731. closure->script_options()->script_sections()->data_segment_relro_end();
  2732. }
  2733. // Create a new list of string/sort pairs.
  2734. extern "C" String_sort_list_ptr
  2735. script_new_string_sort_list(const struct Wildcard_section* string_sort)
  2736. {
  2737. return new String_sort_list(1, *string_sort);
  2738. }
  2739. // Add an entry to a list of string/sort pairs. The way the parser
  2740. // works permits us to simply modify the first parameter, rather than
  2741. // copy the vector.
  2742. extern "C" String_sort_list_ptr
  2743. script_string_sort_list_add(String_sort_list_ptr pv,
  2744. const struct Wildcard_section* string_sort)
  2745. {
  2746. if (pv == NULL)
  2747. return script_new_string_sort_list(string_sort);
  2748. else
  2749. {
  2750. pv->push_back(*string_sort);
  2751. return pv;
  2752. }
  2753. }
  2754. // Create a new list of strings.
  2755. extern "C" String_list_ptr
  2756. script_new_string_list(const char* str, size_t len)
  2757. {
  2758. return new String_list(1, std::string(str, len));
  2759. }
  2760. // Add an element to a list of strings. The way the parser works
  2761. // permits us to simply modify the first parameter, rather than copy
  2762. // the vector.
  2763. extern "C" String_list_ptr
  2764. script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
  2765. {
  2766. if (pv == NULL)
  2767. return script_new_string_list(str, len);
  2768. else
  2769. {
  2770. pv->push_back(std::string(str, len));
  2771. return pv;
  2772. }
  2773. }
  2774. // Concatenate two string lists. Either or both may be NULL. The way
  2775. // the parser works permits us to modify the parameters, rather than
  2776. // copy the vector.
  2777. extern "C" String_list_ptr
  2778. script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
  2779. {
  2780. if (pv1 == NULL)
  2781. return pv2;
  2782. if (pv2 == NULL)
  2783. return pv1;
  2784. pv1->insert(pv1->end(), pv2->begin(), pv2->end());
  2785. return pv1;
  2786. }
  2787. // Add a new program header.
  2788. extern "C" void
  2789. script_add_phdr(void* closurev, const char* name, size_t namelen,
  2790. unsigned int type, const Phdr_info* info)
  2791. {
  2792. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2793. bool includes_filehdr = info->includes_filehdr != 0;
  2794. bool includes_phdrs = info->includes_phdrs != 0;
  2795. bool is_flags_valid = info->is_flags_valid != 0;
  2796. Script_sections* ss = closure->script_options()->script_sections();
  2797. ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs,
  2798. is_flags_valid, info->flags, info->load_address);
  2799. closure->clear_skip_on_incompatible_target();
  2800. }
  2801. // Convert a program header string to a type.
  2802. #define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
  2803. static struct
  2804. {
  2805. const char* name;
  2806. size_t namelen;
  2807. unsigned int val;
  2808. } phdr_type_names[] =
  2809. {
  2810. PHDR_TYPE(PT_NULL),
  2811. PHDR_TYPE(PT_LOAD),
  2812. PHDR_TYPE(PT_DYNAMIC),
  2813. PHDR_TYPE(PT_INTERP),
  2814. PHDR_TYPE(PT_NOTE),
  2815. PHDR_TYPE(PT_SHLIB),
  2816. PHDR_TYPE(PT_PHDR),
  2817. PHDR_TYPE(PT_TLS),
  2818. PHDR_TYPE(PT_GNU_EH_FRAME),
  2819. PHDR_TYPE(PT_GNU_STACK),
  2820. PHDR_TYPE(PT_GNU_RELRO)
  2821. };
  2822. extern "C" unsigned int
  2823. script_phdr_string_to_type(void* closurev, const char* name, size_t namelen)
  2824. {
  2825. for (unsigned int i = 0;
  2826. i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]);
  2827. ++i)
  2828. if (namelen == phdr_type_names[i].namelen
  2829. && strncmp(name, phdr_type_names[i].name, namelen) == 0)
  2830. return phdr_type_names[i].val;
  2831. yyerror(closurev, _("unknown PHDR type (try integer)"));
  2832. return elfcpp::PT_NULL;
  2833. }
  2834. extern "C" void
  2835. script_saw_segment_start_expression(void* closurev)
  2836. {
  2837. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2838. Script_sections* ss = closure->script_options()->script_sections();
  2839. ss->set_saw_segment_start_expression(true);
  2840. }
  2841. extern "C" void
  2842. script_set_section_region(void* closurev, const char* name, size_t namelen,
  2843. int set_vma)
  2844. {
  2845. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2846. if (!closure->script_options()->saw_sections_clause())
  2847. {
  2848. gold_error(_("%s:%d:%d: MEMORY region '%.*s' referred to outside of "
  2849. "SECTIONS clause"),
  2850. closure->filename(), closure->lineno(), closure->charpos(),
  2851. static_cast<int>(namelen), name);
  2852. return;
  2853. }
  2854. Script_sections* ss = closure->script_options()->script_sections();
  2855. Memory_region* mr = ss->find_memory_region(name, namelen);
  2856. if (mr == NULL)
  2857. {
  2858. gold_error(_("%s:%d:%d: MEMORY region '%.*s' not declared"),
  2859. closure->filename(), closure->lineno(), closure->charpos(),
  2860. static_cast<int>(namelen), name);
  2861. return;
  2862. }
  2863. ss->set_memory_region(mr, set_vma);
  2864. }
  2865. extern "C" void
  2866. script_add_memory(void* closurev, const char* name, size_t namelen,
  2867. unsigned int attrs, Expression* origin, Expression* length)
  2868. {
  2869. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2870. Script_sections* ss = closure->script_options()->script_sections();
  2871. ss->add_memory_region(name, namelen, attrs, origin, length);
  2872. }
  2873. extern "C" unsigned int
  2874. script_parse_memory_attr(void* closurev, const char* attrs, size_t attrlen,
  2875. int invert)
  2876. {
  2877. int attributes = 0;
  2878. while (attrlen--)
  2879. switch (*attrs++)
  2880. {
  2881. case 'R':
  2882. case 'r':
  2883. attributes |= MEM_READABLE; break;
  2884. case 'W':
  2885. case 'w':
  2886. attributes |= MEM_READABLE | MEM_WRITEABLE; break;
  2887. case 'X':
  2888. case 'x':
  2889. attributes |= MEM_EXECUTABLE; break;
  2890. case 'A':
  2891. case 'a':
  2892. attributes |= MEM_ALLOCATABLE; break;
  2893. case 'I':
  2894. case 'i':
  2895. case 'L':
  2896. case 'l':
  2897. attributes |= MEM_INITIALIZED; break;
  2898. default:
  2899. yyerror(closurev, _("unknown MEMORY attribute"));
  2900. }
  2901. if (invert)
  2902. attributes = (~ attributes) & MEM_ATTR_MASK;
  2903. return attributes;
  2904. }
  2905. extern "C" void
  2906. script_include_directive(int first_token, void* closurev,
  2907. const char* filename, size_t length)
  2908. {
  2909. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2910. std::string name(filename, length);
  2911. Command_line* cmdline = closure->command_line();
  2912. read_script_file(name.c_str(), cmdline, &cmdline->script_options(),
  2913. first_token, Lex::LINKER_SCRIPT);
  2914. }
  2915. // Functions for memory regions.
  2916. extern "C" Expression*
  2917. script_exp_function_origin(void* closurev, const char* name, size_t namelen)
  2918. {
  2919. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2920. Script_sections* ss = closure->script_options()->script_sections();
  2921. Expression* origin = ss->find_memory_region_origin(name, namelen);
  2922. if (origin == NULL)
  2923. {
  2924. gold_error(_("undefined memory region '%s' referenced "
  2925. "in ORIGIN expression"),
  2926. name);
  2927. // Create a dummy expression to prevent crashes later on.
  2928. origin = script_exp_integer(0);
  2929. }
  2930. return origin;
  2931. }
  2932. extern "C" Expression*
  2933. script_exp_function_length(void* closurev, const char* name, size_t namelen)
  2934. {
  2935. Parser_closure* closure = static_cast<Parser_closure*>(closurev);
  2936. Script_sections* ss = closure->script_options()->script_sections();
  2937. Expression* length = ss->find_memory_region_length(name, namelen);
  2938. if (length == NULL)
  2939. {
  2940. gold_error(_("undefined memory region '%s' referenced "
  2941. "in LENGTH expression"),
  2942. name);
  2943. // Create a dummy expression to prevent crashes later on.
  2944. length = script_exp_integer(0);
  2945. }
  2946. return length;
  2947. }