mmo.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324
  1. /* BFD back-end for mmo objects (MMIX-specific object-format).
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. Written by Hans-Peter Nilsson (hp@bitrange.com).
  4. Infrastructure and other bits originally copied from srec.c and
  5. binary.c.
  6. This file is part of BFD, the Binary File Descriptor library.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. MA 02110-1301, USA. */
  19. /*
  20. SECTION
  21. mmo backend
  22. The mmo object format is used exclusively together with Professor
  23. Donald E.@: Knuth's educational 64-bit processor MMIX. The simulator
  24. @command{mmix} which is available at
  25. @url{http://mmix.cs.hm.edu/src/index.html}
  26. understands this format. That package also includes a combined
  27. assembler and linker called @command{mmixal}. The mmo format has
  28. no advantages feature-wise compared to e.g. ELF. It is a simple
  29. non-relocatable object format with no support for archives or
  30. debugging information, except for symbol value information and
  31. line numbers (which is not yet implemented in BFD). See
  32. @url{http://mmix.cs.hm.edu/} for more
  33. information about MMIX. The ELF format is used for intermediate
  34. object files in the BFD implementation.
  35. @c We want to xref the symbol table node. A feature in "chew"
  36. @c requires that "commands" do not contain spaces in the
  37. @c arguments. Hence the hyphen in "Symbol-table".
  38. @menu
  39. @* File layout::
  40. @* Symbol-table::
  41. @* mmo section mapping::
  42. @end menu
  43. INODE
  44. File layout, Symbol-table, mmo, mmo
  45. SUBSECTION
  46. File layout
  47. The mmo file contents is not partitioned into named sections as
  48. with e.g.@: ELF. Memory areas is formed by specifying the
  49. location of the data that follows. Only the memory area
  50. @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} is executable, so
  51. it is used for code (and constants) and the area
  52. @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} is used for
  53. writable data. @xref{mmo section mapping}.
  54. There is provision for specifying ``special data'' of 65536
  55. different types. We use type 80 (decimal), arbitrarily chosen the
  56. same as the ELF <<e_machine>> number for MMIX, filling it with
  57. section information normally found in ELF objects. @xref{mmo
  58. section mapping}.
  59. Contents is entered as 32-bit words, xor:ed over previous
  60. contents, always zero-initialized. A word that starts with the
  61. byte @samp{0x98} forms a command called a @samp{lopcode}, where
  62. the next byte distinguished between the thirteen lopcodes. The
  63. two remaining bytes, called the @samp{Y} and @samp{Z} fields, or
  64. the @samp{YZ} field (a 16-bit big-endian number), are used for
  65. various purposes different for each lopcode. As documented in
  66. @url{http://mmix.cs.hm.edu/doc/mmixal.pdf},
  67. the lopcodes are:
  68. @table @code
  69. @item lop_quote
  70. 0x98000001. The next word is contents, regardless of whether it
  71. starts with 0x98 or not.
  72. @item lop_loc
  73. 0x9801YYZZ, where @samp{Z} is 1 or 2. This is a location
  74. directive, setting the location for the next data to the next
  75. 32-bit word (for @math{Z = 1}) or 64-bit word (for @math{Z = 2}),
  76. plus @math{Y * 2^56}. Normally @samp{Y} is 0 for the text segment
  77. and 2 for the data segment. Beware that the low bits of non-
  78. tetrabyte-aligned values are silently discarded when being
  79. automatically incremented and when storing contents (in contrast
  80. to e.g. its use as current location when followed by lop_fixo
  81. et al before the next possibly-quoted tetrabyte contents).
  82. @item lop_skip
  83. 0x9802YYZZ. Increase the current location by @samp{YZ} bytes.
  84. @item lop_fixo
  85. 0x9803YYZZ, where @samp{Z} is 1 or 2. Store the current location
  86. as 64 bits into the location pointed to by the next 32-bit
  87. (@math{Z = 1}) or 64-bit (@math{Z = 2}) word, plus @math{Y *
  88. 2^56}.
  89. @item lop_fixr
  90. 0x9804YYZZ. @samp{YZ} is stored into the current location plus
  91. @math{2 - 4 * YZ}.
  92. @item lop_fixrx
  93. 0x980500ZZ. @samp{Z} is 16 or 24. A value @samp{L} derived from
  94. the following 32-bit word are used in a manner similar to
  95. @samp{YZ} in lop_fixr: it is xor:ed into the current location
  96. minus @math{4 * L}. The first byte of the word is 0 or 1. If it
  97. is 1, then @math{L = (@var{lowest 24 bits of word}) - 2^Z}, if 0,
  98. then @math{L = (@var{lowest 24 bits of word})}.
  99. @item lop_file
  100. 0x9806YYZZ. @samp{Y} is the file number, @samp{Z} is count of
  101. 32-bit words. Set the file number to @samp{Y} and the line
  102. counter to 0. The next @math{Z * 4} bytes contain the file name,
  103. padded with zeros if the count is not a multiple of four. The
  104. same @samp{Y} may occur multiple times, but @samp{Z} must be 0 for
  105. all but the first occurrence.
  106. @item lop_line
  107. 0x9807YYZZ. @samp{YZ} is the line number. Together with
  108. lop_file, it forms the source location for the next 32-bit word.
  109. Note that for each non-lopcode 32-bit word, line numbers are
  110. assumed incremented by one.
  111. @item lop_spec
  112. 0x9808YYZZ. @samp{YZ} is the type number. Data until the next
  113. lopcode other than lop_quote forms special data of type @samp{YZ}.
  114. @xref{mmo section mapping}.
  115. Other types than 80, (or type 80 with a content that does not
  116. parse) is stored in sections named <<.MMIX.spec_data.@var{n}>>
  117. where @var{n} is the @samp{YZ}-type. The flags for such a
  118. sections say not to allocate or load the data. The vma is 0.
  119. Contents of multiple occurrences of special data @var{n} is
  120. concatenated to the data of the previous lop_spec @var{n}s. The
  121. location in data or code at which the lop_spec occurred is lost.
  122. @item lop_pre
  123. 0x980901ZZ. The first lopcode in a file. The @samp{Z} field forms the
  124. length of header information in 32-bit words, where the first word
  125. tells the time in seconds since @samp{00:00:00 GMT Jan 1 1970}.
  126. @item lop_post
  127. 0x980a00ZZ. @math{Z > 32}. This lopcode follows after all
  128. content-generating lopcodes in a program. The @samp{Z} field
  129. denotes the value of @samp{rG} at the beginning of the program.
  130. The following @math{256 - Z} big-endian 64-bit words are loaded
  131. into global registers @samp{$G} @dots{} @samp{$255}.
  132. @item lop_stab
  133. 0x980b0000. The next-to-last lopcode in a program. Must follow
  134. immediately after the lop_post lopcode and its data. After this
  135. lopcode follows all symbols in a compressed format
  136. (@pxref{Symbol-table}).
  137. @item lop_end
  138. 0x980cYYZZ. The last lopcode in a program. It must follow the
  139. lop_stab lopcode and its data. The @samp{YZ} field contains the
  140. number of 32-bit words of symbol table information after the
  141. preceding lop_stab lopcode.
  142. @end table
  143. Note that the lopcode "fixups"; <<lop_fixr>>, <<lop_fixrx>> and
  144. <<lop_fixo>> are not generated by BFD, but are handled. They are
  145. generated by <<mmixal>>.
  146. EXAMPLE
  147. This trivial one-label, one-instruction file:
  148. | :Main TRAP 1,2,3
  149. can be represented this way in mmo:
  150. | 0x98090101 - lop_pre, one 32-bit word with timestamp.
  151. | <timestamp>
  152. | 0x98010002 - lop_loc, text segment, using a 64-bit address.
  153. | Note that mmixal does not emit this for the file above.
  154. | 0x00000000 - Address, high 32 bits.
  155. | 0x00000000 - Address, low 32 bits.
  156. | 0x98060002 - lop_file, 2 32-bit words for file-name.
  157. | 0x74657374 - "test"
  158. | 0x2e730000 - ".s\0\0"
  159. | 0x98070001 - lop_line, line 1.
  160. | 0x00010203 - TRAP 1,2,3
  161. | 0x980a00ff - lop_post, setting $255 to 0.
  162. | 0x00000000
  163. | 0x00000000
  164. | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
  165. | 0x203a4040 @xref{Symbol-table}.
  166. | 0x10404020
  167. | 0x4d206120
  168. | 0x69016e00
  169. | 0x81000000
  170. | 0x980c0005 - lop_end; symbol table contained five 32-bit words. */
  171. #include "sysdep.h"
  172. #include "bfd.h"
  173. #include "libbfd.h"
  174. #include "libiberty.h"
  175. #include "elf/mmix.h"
  176. #include "opcode/mmix.h"
  177. #define LOP 0x98
  178. #define LOP_QUOTE 0
  179. #define LOP_LOC 1
  180. #define LOP_SKIP 2
  181. #define LOP_FIXO 3
  182. #define LOP_FIXR 4
  183. #define LOP_FIXRX 5
  184. #define LOP_FILE 6
  185. #define LOP_LINE 7
  186. #define LOP_SPEC 8
  187. #define LOP_PRE 9
  188. #define LOP_POST 10
  189. #define LOP_STAB 11
  190. #define LOP_END 12
  191. #define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
  192. #define SPEC_DATA_SECTION 80
  193. #define LOP_SPEC_SECTION \
  194. ((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)
  195. /* Must be a power of two. If you change this to be >= 64k, you need a
  196. new test-case; the ld test b-loc64k.d touches chunk-size problem areas. */
  197. #define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)
  198. /* An arbitrary number for the maximum length section name size. */
  199. #define MAX_SECTION_NAME_SIZE (1024 * 1024)
  200. /* A quite arbitrary number for the maximum length section size. */
  201. #define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
  202. #define MMO3_WCHAR 0x80
  203. #define MMO3_LEFT 0x40
  204. #define MMO3_MIDDLE 0x20
  205. #define MMO3_RIGHT 0x10
  206. #define MMO3_TYPEBITS 0xf
  207. #define MMO3_REGQUAL_BITS 0xf
  208. #define MMO3_UNDEF 2
  209. #define MMO3_DATA 8
  210. #define MMO3_SYMBITS 0x2f
  211. /* Put these everywhere in new code. */
  212. #define FATAL_DEBUG \
  213. _bfd_abort (__FILE__, __LINE__, \
  214. "Internal: Non-debugged code (test-case missing)")
  215. #define BAD_CASE(x) \
  216. _bfd_abort (__FILE__, __LINE__, \
  217. "bad case for " #x)
  218. enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};
  219. /* When scanning the mmo file, a linked list of mmo_symbol
  220. structures is built to represent the symbol table (if there is
  221. one). */
  222. struct mmo_symbol
  223. {
  224. struct mmo_symbol *next;
  225. char *name;
  226. bfd_vma value;
  227. enum mmo_sym_type sym_type;
  228. unsigned int serno;
  229. };
  230. struct mmo_data_list_struct
  231. {
  232. struct mmo_data_list_struct *next;
  233. bfd_vma where;
  234. bfd_size_type size;
  235. bfd_size_type allocated_size;
  236. bfd_byte data[1];
  237. };
  238. typedef struct mmo_data_list_struct mmo_data_list_type;
  239. struct mmo_symbol_trie
  240. {
  241. struct mmo_symbol_trie *left;
  242. struct mmo_symbol_trie *right;
  243. struct mmo_symbol_trie *middle;
  244. bfd_byte symchar;
  245. /* A zero name means there's nothing here. */
  246. struct mmo_symbol sym;
  247. };
  248. /* The mmo tdata information. */
  249. struct mmo_data_struct
  250. {
  251. struct mmo_symbol *symbols;
  252. struct mmo_symbol *symtail;
  253. asymbol *csymbols;
  254. /* File representation of time (NULL) when this file was created. */
  255. bfd_byte created[4];
  256. /* When we're reading bytes recursively, check this occasionally.
  257. Also holds write errors. */
  258. bfd_boolean have_error;
  259. /* Max symbol length that may appear in the lop_stab table. Note that
  260. this table might just hold a subset of symbols for not-really large
  261. programs, as it can only be 65536 * 4 bytes large. */
  262. int max_symbol_length;
  263. /* Here's the symbol we build in lop_stab. */
  264. char *lop_stab_symbol;
  265. /* Index into lop_stab_symbol for the next character when parsing the
  266. symbol information. */
  267. int symbol_position;
  268. /* When creating arbitrary sections, we need to count section numbers. */
  269. int sec_no;
  270. /* When writing or reading byte-wise, we need to count the bytes
  271. within a 32-bit word. */
  272. int byte_no;
  273. /* We also need a buffer to hold the bytes we count reading or writing. */
  274. bfd_byte buf[4];
  275. };
  276. typedef struct mmo_data_struct tdata_type;
  277. struct mmo_section_data_struct
  278. {
  279. mmo_data_list_type *head;
  280. mmo_data_list_type *tail;
  281. };
  282. #define mmo_section_data(sec) \
  283. ((struct mmo_section_data_struct *) (sec)->used_by_bfd)
  284. /* These structures are used in bfd_map_over_sections constructs. */
  285. /* Used when writing out sections; all but the register contents section
  286. which is stored in reg_section. */
  287. struct mmo_write_sec_info
  288. {
  289. asection *reg_section;
  290. bfd_boolean retval;
  291. };
  292. /* Used when trying to find a section corresponding to addr. */
  293. struct mmo_find_sec_info
  294. {
  295. asection *sec;
  296. bfd_vma addr;
  297. };
  298. static bfd_boolean mmo_bfd_copy_private_bfd_data (bfd *, bfd *);
  299. static void mmo_write_section_unless_reg_contents (bfd *, asection *, void *);
  300. static void mmo_find_sec_w_addr (bfd *, asection *, void *);
  301. static void mmo_find_sec_w_addr_grow (bfd *, asection *, void *);
  302. static asection *mmo_make_section (bfd *, const char *);
  303. static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
  304. static void mmo_print_symbol (bfd *, void *, asymbol *,
  305. bfd_print_symbol_type);
  306. static void mmo_init (void);
  307. static bfd_boolean mmo_mkobject (bfd *);
  308. static bfd_boolean mmo_scan (bfd *);
  309. static asection *mmo_decide_section (bfd *, bfd_vma);
  310. static asection *mmo_get_generic_spec_data_section (bfd *, int);
  311. static asection *mmo_get_spec_section (bfd *, int);
  312. static INLINE bfd_byte *mmo_get_loc (asection *, bfd_vma, int);
  313. static void mmo_xore_64 (asection *, bfd_vma vma, bfd_vma value);
  314. static void mmo_xore_32 (asection *, bfd_vma vma, unsigned int);
  315. static void mmo_xore_16 (asection *, bfd_vma vma, unsigned int);
  316. static const bfd_target *mmo_object_p (bfd *);
  317. static void mmo_map_set_sizes (bfd *, asection *, void *);
  318. static bfd_boolean mmo_get_symbols (bfd *);
  319. static bfd_boolean mmo_create_symbol (bfd *, const char *, bfd_vma,
  320. enum mmo_sym_type, unsigned int);
  321. static bfd_boolean mmo_get_section_contents (bfd *, asection *, void *,
  322. file_ptr, bfd_size_type);
  323. static long mmo_get_symtab_upper_bound (bfd *);
  324. static long mmo_canonicalize_symtab (bfd *, asymbol **);
  325. static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
  326. static void mmo_print_symbol (bfd *, void *, asymbol *,
  327. bfd_print_symbol_type);
  328. static bfd_boolean mmo_set_section_contents (bfd *, sec_ptr, const void *,
  329. file_ptr, bfd_size_type);
  330. static int mmo_sizeof_headers (bfd *, struct bfd_link_info *);
  331. static bfd_boolean mmo_internal_write_header (bfd *);
  332. static bfd_boolean mmo_internal_write_post (bfd *, int, asection *);
  333. static bfd_boolean mmo_internal_add_3_sym (bfd *, struct mmo_symbol_trie *,
  334. const struct mmo_symbol *);
  335. static unsigned int mmo_internal_3_length (bfd *, struct mmo_symbol_trie *);
  336. static void mmo_internal_3_dump (bfd *, struct mmo_symbol_trie *);
  337. static void mmo_beb128_out (bfd *, int, int);
  338. static bfd_boolean mmo_internal_write_section (bfd *, asection *);
  339. static void mmo_write_tetra (bfd *, unsigned int);
  340. static void mmo_write_tetra_raw (bfd *, unsigned int);
  341. static void mmo_write_octa (bfd *, bfd_vma);
  342. static void mmo_write_octa_raw (bfd *, bfd_vma);
  343. static bfd_boolean mmo_write_chunk (bfd *, const bfd_byte *, unsigned int);
  344. static bfd_boolean mmo_flush_chunk (bfd *);
  345. static bfd_boolean mmo_write_loc_chunk (bfd *, bfd_vma, const bfd_byte *,
  346. unsigned int, bfd_vma *);
  347. static bfd_boolean mmo_write_chunk_list (bfd *, mmo_data_list_type *);
  348. static bfd_boolean mmo_write_loc_chunk_list (bfd *, mmo_data_list_type *);
  349. static bfd_boolean mmo_write_symbols_and_terminator (bfd *);
  350. static flagword mmo_sec_flags_from_bfd_flags (flagword);
  351. static flagword bfd_sec_flags_from_mmo_flags (flagword);
  352. static bfd_byte mmo_get_byte (bfd *);
  353. static void mmo_write_byte (bfd *, bfd_byte);
  354. static bfd_boolean mmo_new_section_hook (bfd *, asection *);
  355. static int mmo_sort_mmo_symbols (const void *, const void *);
  356. static bfd_boolean mmo_write_object_contents (bfd *);
  357. static bfd_boolean mmo_write_section_description (bfd *, asection *);
  358. static bfd_boolean mmo_has_leading_or_trailing_zero_tetra_p (bfd *,
  359. asection *);
  360. /* Global "const" variables initialized once. Must not depend on
  361. particular input or caller; put such things into the bfd or elsewhere.
  362. Look ma, no static per-invocation data! */
  363. static
  364. char valid_mmo_symbol_character_set[/* A-Z a-z (we assume consecutive
  365. codes; sorry EBCDIC:ers!). */
  366. + 'Z' - 'A' + 1 + 'z' - 'a' + 1
  367. /* Digits. */
  368. + 10
  369. /* ':' and '_'. */
  370. + 1 + 1
  371. /* Codes higher than 126. */
  372. + 256 - 126
  373. /* Ending zero. */
  374. + 1];
  375. /* Get section SECNAME or create one if it doesn't exist. When creating
  376. one, new memory for the name is allocated. */
  377. static asection *
  378. mmo_make_section (bfd *abfd, const char *secname)
  379. {
  380. asection *sec = bfd_get_section_by_name (abfd, secname);
  381. if (sec == NULL)
  382. {
  383. char *newsecname = strdup (secname);
  384. if (newsecname == NULL)
  385. {
  386. (*_bfd_error_handler)
  387. (_("%s: No core to allocate section name %s\n"),
  388. bfd_get_filename (abfd), secname);
  389. bfd_set_error (bfd_error_system_call);
  390. return NULL;
  391. }
  392. sec = bfd_make_section (abfd, newsecname);
  393. }
  394. return sec;
  395. }
  396. /* Nothing to do, but keep as a placeholder if we need it.
  397. Note that state that might differ between bfd:s must not be initialized
  398. here, nor must it be static. Add it to tdata information instead. */
  399. static void
  400. mmo_init (void)
  401. {
  402. static bfd_boolean inited = FALSE;
  403. int i = 0;
  404. int j = 0;
  405. static const char letters[]
  406. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
  407. if (inited)
  408. return;
  409. inited = TRUE;
  410. /* Fill in the set of valid symbol characters. */
  411. strcpy (valid_mmo_symbol_character_set, letters);
  412. i = strlen (letters);
  413. for (j = 126; j < 256; j++)
  414. valid_mmo_symbol_character_set[i++] = j;
  415. }
  416. /* Check whether an existing file is an mmo file. */
  417. static const bfd_target *
  418. mmo_object_p (bfd *abfd)
  419. {
  420. struct stat statbuf;
  421. bfd_byte b[4];
  422. mmo_init ();
  423. if (bfd_stat (abfd, &statbuf) < 0
  424. || bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  425. || bfd_bread (b, 4, abfd) != 4)
  426. goto bad_final;
  427. /* All mmo files are a multiple of four bytes long.
  428. Only recognize version one. */
  429. if ((statbuf.st_size % 4) != 0
  430. || b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
  431. goto bad_format;
  432. /* Get the last 32-bit word. */
  433. if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
  434. || bfd_bread (b, 4, abfd) != 4)
  435. goto bad_final;
  436. /* Check if the file ends in a lop_end lopcode. */
  437. if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
  438. goto bad_format;
  439. /* Compute an upper bound on the max symbol length. Not really
  440. important as all of the symbol information can only be 256k. */
  441. abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
  442. abfd->tdata.mmo_data->lop_stab_symbol
  443. = bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
  444. if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
  445. {
  446. (*_bfd_error_handler)
  447. (_("%s: No core to allocate a symbol %d bytes long\n"),
  448. bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
  449. goto bad_final;
  450. }
  451. /* Read in everything. */
  452. if (! mmo_scan (abfd))
  453. goto bad_format_free;
  454. if (abfd->symcount > 0)
  455. abfd->flags |= HAS_SYMS;
  456. /* You'll have to tweak this if you want to use this format for other
  457. arches (not recommended due to its small-size limitations). Look at
  458. the ELF format for how to make it target-generic. */
  459. if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
  460. goto bad_format_free;
  461. return abfd->xvec;
  462. bad_format_free:
  463. free (abfd->tdata.mmo_data->lop_stab_symbol);
  464. bad_format:
  465. bfd_set_error (bfd_error_wrong_format);
  466. bad_final:
  467. return NULL;
  468. }
  469. /* Set up the mmo tdata information. */
  470. static bfd_boolean
  471. mmo_mkobject (bfd *abfd)
  472. {
  473. mmo_init ();
  474. if (abfd->tdata.mmo_data == NULL)
  475. {
  476. time_t created;
  477. /* All fields are zero-initialized, so we don't have to explicitly
  478. initialize most. */
  479. tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
  480. if (tdata == NULL)
  481. return FALSE;
  482. created = time (NULL);
  483. bfd_put_32 (abfd, created, tdata->created);
  484. abfd->tdata.mmo_data = tdata;
  485. }
  486. return TRUE;
  487. }
  488. static bfd_boolean
  489. mmo_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  490. {
  491. if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
  492. || bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
  493. return TRUE;
  494. /* Copy the time the copied-from file was created. If people want the
  495. time the file was last *modified*, they have that in the normal file
  496. information. */
  497. memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
  498. sizeof (obfd->tdata.mmo_data->created));
  499. return TRUE;
  500. }
  501. /* Helper functions for mmo_decide_section, used through
  502. bfd_map_over_sections. */
  503. static void
  504. mmo_find_sec_w_addr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
  505. {
  506. struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
  507. bfd_vma vma = bfd_get_section_vma (abfd, sec);
  508. /* Ignore sections that aren't loaded. */
  509. if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
  510. != (SEC_LOAD | SEC_ALLOC))
  511. return;
  512. if (infop->addr >= vma && infop->addr < vma + sec->size)
  513. infop->sec = sec;
  514. }
  515. static void
  516. mmo_find_sec_w_addr_grow (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
  517. {
  518. struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
  519. bfd_vma vma = bfd_get_section_vma (abfd, sec);
  520. /* Ignore sections that aren't loaded. */
  521. if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
  522. != (SEC_LOAD | SEC_ALLOC))
  523. return;
  524. if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
  525. infop->sec = sec;
  526. }
  527. /* Find a section that corresponds to a VMA. Automatically create .text
  528. or .data and set current section to it, depending on what vma. If we
  529. can't deduce a section, make one up as ".MMIX.sec.N", where N is an
  530. increasing number. */
  531. static asection *
  532. mmo_decide_section (bfd *abfd, bfd_vma vma)
  533. {
  534. asection *sec = NULL;
  535. char sec_name[sizeof (".MMIX.sec.") + 20];
  536. struct mmo_find_sec_info info;
  537. info.addr = vma;
  538. info.sec = NULL;
  539. /* First see if there's a section that would match exactly. */
  540. bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
  541. if (info.sec != NULL)
  542. return info.sec;
  543. /* If there's no such section, try and expand one of the existing ones,
  544. up to a limit. Make sure we have .text and .data before we try that;
  545. create them corresponding to expected addresses and set flags to make
  546. them match the "loaded and with contents" expectation. */
  547. if ((vma >> 56) == 0)
  548. {
  549. sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
  550. if (sec == NULL)
  551. return NULL;
  552. if (! sec->user_set_vma && ! bfd_set_section_vma (abfd, sec, vma))
  553. return NULL;
  554. if (! bfd_set_section_flags (abfd, sec,
  555. bfd_get_section_flags (abfd, sec)
  556. | SEC_CODE | SEC_LOAD | SEC_ALLOC))
  557. return NULL;
  558. }
  559. else if ((vma >> 56) == 0x20)
  560. {
  561. sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
  562. if (sec == NULL)
  563. return NULL;
  564. if (! sec->user_set_vma && ! bfd_set_section_vma (abfd, sec, vma))
  565. return NULL;
  566. if (! bfd_set_section_flags (abfd, sec,
  567. bfd_get_section_flags (abfd, sec)
  568. | SEC_LOAD | SEC_ALLOC))
  569. return NULL;
  570. }
  571. bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
  572. if (info.sec != NULL)
  573. return info.sec;
  574. /* If there's still no suitable section, make a new one. */
  575. sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
  576. sec = mmo_make_section (abfd, sec_name);
  577. if (! sec->user_set_vma && ! bfd_set_section_vma (abfd, sec, vma))
  578. return NULL;
  579. if (! bfd_set_section_flags (abfd, sec,
  580. bfd_get_section_flags (abfd, sec)
  581. | SEC_LOAD | SEC_ALLOC))
  582. return NULL;
  583. return sec;
  584. }
  585. /* Xor in a 64-bit value VALUE at VMA. */
  586. static INLINE void
  587. mmo_xore_64 (asection *sec, bfd_vma vma, bfd_vma value)
  588. {
  589. bfd_byte *loc = mmo_get_loc (sec, vma, 8);
  590. bfd_vma prev = bfd_get_64 (sec->owner, loc);
  591. value ^= prev;
  592. bfd_put_64 (sec->owner, value, loc);
  593. }
  594. /* Xor in a 32-bit value VALUE at VMA. */
  595. static INLINE void
  596. mmo_xore_32 (asection *sec, bfd_vma vma, unsigned int value)
  597. {
  598. bfd_byte *loc = mmo_get_loc (sec, vma, 4);
  599. unsigned int prev = bfd_get_32 (sec->owner, loc);
  600. value ^= prev;
  601. bfd_put_32 (sec->owner, value, loc);
  602. }
  603. /* Xor in a 16-bit value VALUE at VMA. */
  604. static INLINE void
  605. mmo_xore_16 (asection *sec, bfd_vma vma, unsigned int value)
  606. {
  607. bfd_byte *loc = mmo_get_loc (sec, vma, 2);
  608. unsigned int prev = bfd_get_16 (sec->owner, loc);
  609. value ^= prev;
  610. bfd_put_16 (sec->owner, value, loc);
  611. }
  612. /* Write a 32-bit word to output file, no lop_quote generated. */
  613. static INLINE void
  614. mmo_write_tetra_raw (bfd *abfd, unsigned int value)
  615. {
  616. bfd_byte buf[4];
  617. bfd_put_32 (abfd, value, buf);
  618. if (bfd_bwrite (buf, 4, abfd) != 4)
  619. abfd->tdata.mmo_data->have_error = TRUE;
  620. }
  621. /* Write a 32-bit word to output file; lop_quote if necessary. */
  622. static INLINE void
  623. mmo_write_tetra (bfd *abfd, unsigned int value)
  624. {
  625. if (((value >> 24) & 0xff) == LOP)
  626. mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
  627. mmo_write_tetra_raw (abfd, value);
  628. }
  629. /* Write a 64-bit word to output file, perhaps with lop_quoting. */
  630. static INLINE void
  631. mmo_write_octa (bfd *abfd, bfd_vma value)
  632. {
  633. mmo_write_tetra (abfd, (unsigned int) (value >> 32));
  634. mmo_write_tetra (abfd, (unsigned int) value);
  635. }
  636. /* Write a 64-bit word to output file, without lop_quoting. */
  637. static INLINE void
  638. mmo_write_octa_raw (bfd *abfd, bfd_vma value)
  639. {
  640. mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
  641. mmo_write_tetra_raw (abfd, (unsigned int) value);
  642. }
  643. /* Write quoted contents. Intended to be called multiple times in
  644. sequence, followed by a call to mmo_flush_chunk. */
  645. static INLINE bfd_boolean
  646. mmo_write_chunk (bfd *abfd, const bfd_byte *loc, unsigned int len)
  647. {
  648. bfd_boolean retval = TRUE;
  649. struct mmo_data_struct *mmop = abfd->tdata.mmo_data;
  650. /* Fill up a tetra from bytes remaining from a previous chunk. */
  651. if (mmop->byte_no != 0)
  652. {
  653. while (mmop->byte_no < 4 && len != 0)
  654. {
  655. mmop->buf[mmop->byte_no++] = *loc++;
  656. len--;
  657. }
  658. if (mmop->byte_no == 4)
  659. {
  660. mmo_write_tetra (abfd, bfd_get_32 (abfd, mmop->buf));
  661. mmop->byte_no = 0;
  662. }
  663. }
  664. while (len >= 4)
  665. {
  666. if (loc[0] == LOP)
  667. mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
  668. retval = (retval
  669. && ! mmop->have_error
  670. && 4 == bfd_bwrite (loc, 4, abfd));
  671. loc += 4;
  672. len -= 4;
  673. }
  674. if (len)
  675. {
  676. /* We must have flushed a previous remainder if we get one from
  677. this chunk too. */
  678. BFD_ASSERT (mmop->byte_no == 0);
  679. memcpy (mmop->buf, loc, len);
  680. mmop->byte_no = len;
  681. }
  682. if (! retval)
  683. mmop->have_error = TRUE;
  684. return retval;
  685. }
  686. /* Flush remaining bytes, from a previous mmo_write_chunk, zero-padded to
  687. 4 bytes. */
  688. static INLINE bfd_boolean
  689. mmo_flush_chunk (bfd *abfd)
  690. {
  691. if (abfd->tdata.mmo_data->byte_no != 0)
  692. {
  693. memset (abfd->tdata.mmo_data->buf + abfd->tdata.mmo_data->byte_no,
  694. 0, 4 - abfd->tdata.mmo_data->byte_no);
  695. mmo_write_tetra (abfd,
  696. bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
  697. abfd->tdata.mmo_data->byte_no = 0;
  698. }
  699. return ! abfd->tdata.mmo_data->have_error;
  700. }
  701. /* Same, but from a list. */
  702. static INLINE bfd_boolean
  703. mmo_write_chunk_list (bfd *abfd, mmo_data_list_type *datap)
  704. {
  705. for (; datap != NULL; datap = datap->next)
  706. if (! mmo_write_chunk (abfd, datap->data, datap->size))
  707. return FALSE;
  708. return mmo_flush_chunk (abfd);
  709. }
  710. /* Write a lop_loc and some contents. A caller needs to call
  711. mmo_flush_chunk after calling this function. The location is only
  712. output if different than *LAST_VMAP, which is updated after this call. */
  713. static bfd_boolean
  714. mmo_write_loc_chunk (bfd *abfd, bfd_vma vma, const bfd_byte *loc,
  715. unsigned int len, bfd_vma *last_vmap)
  716. {
  717. /* Find an initial and trailing section of zero (aligned) tetras; we don't
  718. need to write out zeros. FIXME: When we do this, we should emit
  719. section size and address specifiers, else objcopy can't always perform
  720. an identity translation. Only do this if we *don't* have left-over
  721. data from a previous write (and will not add any) or else the vma of
  722. this chunk is *not* the next address, because then data isn't
  723. tetrabyte-aligned and we're concatenating to that left-over data. */
  724. if ((vma & 3) == 0
  725. && (abfd->tdata.mmo_data->byte_no == 0 || vma != *last_vmap))
  726. {
  727. while (len > 4 && bfd_get_32 (abfd, loc) == 0)
  728. {
  729. vma += 4;
  730. len -= 4;
  731. loc += 4;
  732. }
  733. if ((len & 3) == 0)
  734. while (len > 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
  735. len -= 4;
  736. }
  737. /* Only write out the location if it's different than the one the caller
  738. (supposedly) previously handled, accounting for omitted leading zeros. */
  739. if (vma != *last_vmap)
  740. {
  741. /* We might be in the middle of a sequence. */
  742. mmo_flush_chunk (abfd);
  743. /* This should not happen during normal usage, but can presumably
  744. happen with an erroneous linker-script, so handle gracefully.
  745. Avoid Knuth-specific terms in the message, such as "tetrabyte".
  746. Note that this function will get non-4-multiple lengths and
  747. unaligned vmas but those come in tuples (mostly pairs) and are
  748. continuous (i.e. the if-condition above false) and they are
  749. group-wise aligned. */
  750. if ((vma & 3) != 0)
  751. {
  752. (*_bfd_error_handler)
  753. (_("%s: attempt to emit contents at non-multiple-of-4 address 0x%lx\n"),
  754. bfd_get_filename (abfd), (unsigned long) vma);
  755. bfd_set_error (bfd_error_bad_value);
  756. return FALSE;
  757. }
  758. /* We always write the location as 64 bits; no use saving bytes
  759. here. */
  760. mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
  761. mmo_write_octa_raw (abfd, vma);
  762. }
  763. /* Update to reflect end of this chunk, with trailing zeros omitted. */
  764. *last_vmap = vma + len;
  765. return (! abfd->tdata.mmo_data->have_error
  766. && mmo_write_chunk (abfd, loc, len));
  767. }
  768. /* Same, but from a list. */
  769. static INLINE bfd_boolean
  770. mmo_write_loc_chunk_list (bfd *abfd, mmo_data_list_type *datap)
  771. {
  772. /* Get an address different than the address of the first chunk. */
  773. bfd_vma last_vma = datap ? datap->where - 1 : 0;
  774. for (; datap != NULL; datap = datap->next)
  775. if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size,
  776. &last_vma))
  777. return FALSE;
  778. return mmo_flush_chunk (abfd);
  779. }
  780. /* Make a .MMIX.spec_data.N section. */
  781. static asection *
  782. mmo_get_generic_spec_data_section (bfd *abfd, int spec_data_number)
  783. {
  784. asection *sec;
  785. char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
  786. = MMIX_OTHER_SPEC_SECTION_PREFIX;
  787. sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
  788. "%d", spec_data_number);
  789. sec = mmo_make_section (abfd, secname);
  790. return sec;
  791. }
  792. /* Make a special section for SPEC_DATA_NUMBER. If it is the one we use
  793. ourselves, parse some of its data to get at the section name. */
  794. static asection *
  795. mmo_get_spec_section (bfd *abfd, int spec_data_number)
  796. {
  797. char *secname;
  798. asection *sec;
  799. bfd_byte buf[4];
  800. unsigned int secname_length;
  801. unsigned int i;
  802. bfd_vma section_length;
  803. bfd_vma section_vma;
  804. mmo_data_list_type *loc;
  805. flagword flags;
  806. long orig_pos;
  807. /* If this isn't the "special" special data, then make a placeholder
  808. section. */
  809. if (spec_data_number != SPEC_DATA_SECTION)
  810. return mmo_get_generic_spec_data_section (abfd, spec_data_number);
  811. /* Seek back to this position if there was a format error. */
  812. orig_pos = bfd_tell (abfd);
  813. /* Read the length (in 32-bit words). */
  814. if (bfd_bread (buf, 4, abfd) != 4)
  815. goto format_error;
  816. if (buf[0] == LOP)
  817. {
  818. if (buf[1] != LOP_QUOTE)
  819. goto format_error;
  820. if (bfd_bread (buf, 4, abfd) != 4)
  821. goto format_error;
  822. }
  823. /* We don't care to keep the name length accurate. It's
  824. zero-terminated. */
  825. secname_length = bfd_get_32 (abfd, buf) * 4;
  826. /* Check section name length for sanity. */
  827. if (secname_length > MAX_SECTION_NAME_SIZE)
  828. goto format_error;
  829. /* This should be free'd regardless if a section is created. */
  830. secname = bfd_malloc (secname_length + 1);
  831. secname[secname_length] = 0;
  832. for (i = 0; i < secname_length / 4; i++)
  833. {
  834. if (bfd_bread (secname + i * 4, 4, abfd) != 4)
  835. goto format_error_free;
  836. if (secname[i * 4] == (char) LOP)
  837. {
  838. /* A bit of overkill, but we handle char 0x98 in a section name,
  839. and recognize misparsing. */
  840. if (secname[i * 4 + 1] != LOP_QUOTE
  841. || bfd_bread (secname + i * 4, 4, abfd) != 4)
  842. /* Whoops. We thought this was a name, and now we found a
  843. non-lop_quote lopcode before we parsed the whole length of
  844. the name. Signal end-of-file in the same manner. */
  845. goto format_error_free;
  846. }
  847. }
  848. /* Get the section flags. */
  849. if (bfd_bread (buf, 4, abfd) != 4
  850. || (buf[0] == LOP
  851. && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
  852. goto format_error_free;
  853. flags = bfd_get_32 (abfd, buf);
  854. /* Get the section length. */
  855. if (bfd_bread (buf, 4, abfd) != 4
  856. || (buf[0] == LOP
  857. && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
  858. goto format_error_free;
  859. section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
  860. /* That's the first, high-part. Now get the low part. */
  861. if (bfd_bread (buf, 4, abfd) != 4
  862. || (buf[0] == LOP
  863. && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
  864. goto format_error_free;
  865. section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
  866. /* Check the section length for sanity. */
  867. if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
  868. goto format_error_free;
  869. /* Get the section VMA. */
  870. if (bfd_bread (buf, 4, abfd) != 4
  871. || (buf[0] == LOP
  872. && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
  873. goto format_error_free;
  874. section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
  875. /* That's the first, high-part. Now get the low part. */
  876. if (bfd_bread (buf, 4, abfd) != 4
  877. || (buf[0] == LOP
  878. && (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
  879. goto format_error_free;
  880. section_vma |= (bfd_vma) bfd_get_32 (abfd, buf);
  881. sec = mmo_make_section (abfd, secname);
  882. free (secname);
  883. if (sec == NULL)
  884. goto format_error;
  885. /* We allocate a buffer here for the advertised size, with head room for
  886. tetrabyte alignment. */
  887. loc = bfd_zmalloc (section_length + 3
  888. + sizeof (struct mmo_data_list_struct));
  889. if (loc == NULL)
  890. goto format_error;
  891. /* Use a TETRA-rounded size for the allocated buffer; we set the
  892. "visible" section size below. */
  893. loc->size = (section_length + 3) & ~3;
  894. /* Add in the section flags we found to those bfd entered during this
  895. process and set the contents. */
  896. if (! bfd_set_section_flags (abfd, sec,
  897. bfd_sec_flags_from_mmo_flags (flags)
  898. | bfd_get_section_flags (abfd, sec)
  899. | (section_length != 0 ? SEC_HAS_CONTENTS : 0))
  900. || ! bfd_set_section_size (abfd, sec, sec->size + section_length)
  901. /* Set VMA only for the first occurrence. */
  902. || (! sec->user_set_vma
  903. && ! bfd_set_section_vma (abfd, sec, section_vma)))
  904. {
  905. /* If we get an error for any of the calls above, signal more than
  906. just a format error for the spec section. */
  907. return NULL;
  908. }
  909. loc->next = NULL;
  910. if (mmo_section_data (sec)->tail != NULL)
  911. mmo_section_data (sec)->tail->next = loc;
  912. else
  913. mmo_section_data (sec)->head = loc;
  914. mmo_section_data (sec)->tail = loc;
  915. loc->where = section_vma;
  916. return sec;
  917. format_error_free:
  918. free (secname);
  919. format_error:
  920. if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
  921. return NULL;
  922. return mmo_get_generic_spec_data_section (abfd, spec_data_number);
  923. }
  924. /* Read a byte, but read from file in multiples of 32-bit words. */
  925. static bfd_byte
  926. mmo_get_byte (bfd *abfd)
  927. {
  928. bfd_byte retval;
  929. if (abfd->tdata.mmo_data->byte_no == 0)
  930. {
  931. if (! abfd->tdata.mmo_data->have_error
  932. && bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
  933. {
  934. abfd->tdata.mmo_data->have_error = TRUE;
  935. /* A value somewhat safe against tripping on some inconsistency
  936. when mopping up after this error. */
  937. return 128;
  938. }
  939. }
  940. retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
  941. abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
  942. return retval;
  943. }
  944. /* Write a byte, in multiples of 32-bit words. */
  945. static void
  946. mmo_write_byte (bfd *abfd, bfd_byte value)
  947. {
  948. abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
  949. if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
  950. {
  951. if (! abfd->tdata.mmo_data->have_error
  952. && bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
  953. abfd->tdata.mmo_data->have_error = TRUE;
  954. }
  955. }
  956. /* Create a symbol. */
  957. static bfd_boolean
  958. mmo_create_symbol (bfd *abfd, const char *symname, bfd_vma addr, enum
  959. mmo_sym_type sym_type, unsigned int serno)
  960. {
  961. struct mmo_symbol *n;
  962. n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
  963. if (n == NULL)
  964. return FALSE;
  965. n->name = bfd_alloc (abfd, strlen (symname) + 1);
  966. if (n->name == NULL)
  967. return FALSE;
  968. strcpy (n->name, symname);
  969. n->value = addr;
  970. n->sym_type = sym_type;
  971. n->serno = serno;
  972. if (abfd->tdata.mmo_data->symbols == NULL)
  973. abfd->tdata.mmo_data->symbols = n;
  974. else
  975. abfd->tdata.mmo_data->symtail->next = n;
  976. abfd->tdata.mmo_data->symtail = n;
  977. n->next = NULL;
  978. ++abfd->symcount;
  979. /* Check that :Main equals the last octa of the .MMIX.reg_contents
  980. section, as it's the one place we're sure to pass when reading a mmo
  981. object. For written objects, we do it while setting the symbol
  982. table. */
  983. if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
  984. && bfd_get_start_address (abfd) != addr)
  985. {
  986. (*_bfd_error_handler)
  987. (_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
  988. bfd_get_filename (abfd));
  989. bfd_set_error (bfd_error_bad_value);
  990. return FALSE;
  991. }
  992. return TRUE;
  993. }
  994. /* Read in symbols. */
  995. static bfd_boolean
  996. mmo_get_symbols (bfd *abfd)
  997. {
  998. /*
  999. INODE
  1000. Symbol-table, mmo section mapping, File layout, mmo
  1001. SUBSECTION
  1002. Symbol table format
  1003. From mmixal.w (or really, the generated mmixal.tex) in the
  1004. MMIXware package which also contains the @command{mmix} simulator:
  1005. ``Symbols are stored and retrieved by means of a @samp{ternary
  1006. search trie}, following ideas of Bentley and Sedgewick. (See
  1007. ACM--SIAM Symp.@: on Discrete Algorithms @samp{8} (1997), 360--369;
  1008. R.@:Sedgewick, @samp{Algorithms in C} (Reading, Mass.@:
  1009. Addison--Wesley, 1998), @samp{15.4}.) Each trie node stores a
  1010. character, and there are branches to subtries for the cases where
  1011. a given character is less than, equal to, or greater than the
  1012. character in the trie. There also is a pointer to a symbol table
  1013. entry if a symbol ends at the current node.''
  1014. So it's a tree encoded as a stream of bytes. The stream of bytes
  1015. acts on a single virtual global symbol, adding and removing
  1016. characters and signalling complete symbol points. Here, we read
  1017. the stream and create symbols at the completion points.
  1018. First, there's a control byte <<m>>. If any of the listed bits
  1019. in <<m>> is nonzero, we execute what stands at the right, in
  1020. the listed order:
  1021. | (MMO3_LEFT)
  1022. | 0x40 - Traverse left trie.
  1023. | (Read a new command byte and recurse.)
  1024. |
  1025. | (MMO3_SYMBITS)
  1026. | 0x2f - Read the next byte as a character and store it in the
  1027. | current character position; increment character position.
  1028. | Test the bits of <<m>>:
  1029. |
  1030. | (MMO3_WCHAR)
  1031. | 0x80 - The character is 16-bit (so read another byte,
  1032. | merge into current character.
  1033. |
  1034. | (MMO3_TYPEBITS)
  1035. | 0xf - We have a complete symbol; parse the type, value
  1036. | and serial number and do what should be done
  1037. | with a symbol. The type and length information
  1038. | is in j = (m & 0xf).
  1039. |
  1040. | (MMO3_REGQUAL_BITS)
  1041. | j == 0xf: A register variable. The following
  1042. | byte tells which register.
  1043. | j <= 8: An absolute symbol. Read j bytes as the
  1044. | big-endian number the symbol equals.
  1045. | A j = 2 with two zero bytes denotes an
  1046. | unknown symbol.
  1047. | j > 8: As with j <= 8, but add (0x20 << 56)
  1048. | to the value in the following j - 8
  1049. | bytes.
  1050. |
  1051. | Then comes the serial number, as a variant of
  1052. | uleb128, but better named ubeb128:
  1053. | Read bytes and shift the previous value left 7
  1054. | (multiply by 128). Add in the new byte, repeat
  1055. | until a byte has bit 7 set. The serial number
  1056. | is the computed value minus 128.
  1057. |
  1058. | (MMO3_MIDDLE)
  1059. | 0x20 - Traverse middle trie. (Read a new command byte
  1060. | and recurse.) Decrement character position.
  1061. |
  1062. | (MMO3_RIGHT)
  1063. | 0x10 - Traverse right trie. (Read a new command byte and
  1064. | recurse.)
  1065. Let's look again at the <<lop_stab>> for the trivial file
  1066. (@pxref{File layout}).
  1067. | 0x980b0000 - lop_stab for ":Main" = 0, serial 1.
  1068. | 0x203a4040
  1069. | 0x10404020
  1070. | 0x4d206120
  1071. | 0x69016e00
  1072. | 0x81000000
  1073. This forms the trivial trie (note that the path between ``:'' and
  1074. ``M'' is redundant):
  1075. | 203a ":"
  1076. | 40 /
  1077. | 40 /
  1078. | 10 \
  1079. | 40 /
  1080. | 40 /
  1081. | 204d "M"
  1082. | 2061 "a"
  1083. | 2069 "i"
  1084. | 016e "n" is the last character in a full symbol, and
  1085. | with a value represented in one byte.
  1086. | 00 The value is 0.
  1087. | 81 The serial number is 1. */
  1088. bfd_byte m = mmo_get_byte (abfd);
  1089. /* Check first if we have a bad hair day. */
  1090. if (abfd->tdata.mmo_data->have_error)
  1091. return FALSE;
  1092. if (m & MMO3_LEFT)
  1093. /* Traverse left trie. */
  1094. mmo_get_symbols (abfd);
  1095. if (m & MMO3_SYMBITS)
  1096. {
  1097. bfd_byte c = mmo_get_byte (abfd);
  1098. bfd_byte j = m & MMO3_TYPEBITS;
  1099. bfd_vma addr = 0;
  1100. enum mmo_sym_type sym_type;
  1101. unsigned int serno = 0;
  1102. bfd_byte k;
  1103. if (m & MMO3_WCHAR)
  1104. {
  1105. bfd_byte c2 = mmo_get_byte (abfd);
  1106. /* A two-byte character. We can't grok this, but neither can
  1107. mmotype, for other cases than the second byte being zero. */
  1108. if (c != 0)
  1109. {
  1110. abfd->tdata.mmo_data->lop_stab_symbol
  1111. [abfd->tdata.mmo_data->symbol_position] = 0;
  1112. (*_bfd_error_handler)
  1113. (_("%s: unsupported wide character sequence"
  1114. " 0x%02X 0x%02X after symbol name starting with `%s'\n"),
  1115. bfd_get_filename (abfd), c, c2,
  1116. abfd->tdata.mmo_data->lop_stab_symbol);
  1117. bfd_set_error (bfd_error_bad_value);
  1118. abfd->tdata.mmo_data->have_error = TRUE;
  1119. return FALSE;
  1120. }
  1121. else
  1122. c = c2;
  1123. }
  1124. abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
  1125. abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
  1126. if (j & MMO3_REGQUAL_BITS)
  1127. {
  1128. if (j == MMO3_REGQUAL_BITS)
  1129. {
  1130. sym_type = mmo_reg_sym;
  1131. addr = mmo_get_byte (abfd);
  1132. }
  1133. else if (j <= 8)
  1134. {
  1135. unsigned int i;
  1136. for (i = 0; i < j; i++)
  1137. addr = (addr << 8) + mmo_get_byte (abfd);
  1138. if (addr == 0 && j == MMO3_UNDEF)
  1139. sym_type = mmo_undef_sym;
  1140. else
  1141. sym_type = mmo_abs_sym;
  1142. }
  1143. else
  1144. {
  1145. unsigned int i;
  1146. for (i = MMO3_DATA; i < j; i++)
  1147. addr = (addr << 8) + mmo_get_byte (abfd);
  1148. addr += (bfd_vma) 0x20 << 56;
  1149. sym_type = mmo_data_sym;
  1150. }
  1151. /* Get the serial number. */
  1152. do
  1153. {
  1154. k = mmo_get_byte (abfd);
  1155. serno = (serno << 7) + k;
  1156. }
  1157. while (k < 128);
  1158. serno -= 128;
  1159. /* Got it. Now enter it. Skip a leading ":". */
  1160. if (! abfd->tdata.mmo_data->have_error
  1161. && ! mmo_create_symbol (abfd,
  1162. abfd->tdata.mmo_data->lop_stab_symbol
  1163. + 1,
  1164. addr, sym_type, serno))
  1165. abfd->tdata.mmo_data->have_error = TRUE;
  1166. }
  1167. if (m & MMO3_MIDDLE)
  1168. /* Traverse middle trie. */
  1169. mmo_get_symbols (abfd);
  1170. abfd->tdata.mmo_data->symbol_position--;
  1171. }
  1172. if (m & MMO3_RIGHT)
  1173. /* Traverse right trie. */
  1174. mmo_get_symbols (abfd);
  1175. return ! abfd->tdata.mmo_data->have_error;
  1176. }
  1177. /* Get the location of memory area [VMA..VMA + SIZE - 1], which we think
  1178. is in section SEC. Adjust and reallocate zero-initialized contents.
  1179. If there's new contents, allocate to the next multiple of
  1180. MMO_SEC_CONTENTS_CHUNK_SIZE. */
  1181. static INLINE bfd_byte *
  1182. mmo_get_loc (asection *sec, bfd_vma vma, int size)
  1183. {
  1184. bfd_size_type allocated_size;
  1185. struct mmo_section_data_struct *sdatap = mmo_section_data (sec);
  1186. struct mmo_data_list_struct *datap = sdatap->head;
  1187. struct mmo_data_list_struct *entry;
  1188. /* First search the list to see if we have the requested chunk in one
  1189. piece, or perhaps if we have a suitable chunk with room to fit. */
  1190. for (; datap != NULL; datap = datap->next)
  1191. {
  1192. if (datap->where <= vma
  1193. && datap->where + datap->size >= vma + size)
  1194. return datap->data + vma - datap->where;
  1195. else if (datap->where <= vma
  1196. && datap->where + datap->allocated_size >= vma + size
  1197. /* Only munch on the "allocated size" if it does not
  1198. overlap the next chunk. */
  1199. && (datap->next == NULL || datap->next->where >= vma + size))
  1200. {
  1201. /* There was room allocated, but the size wasn't set to include
  1202. it. Do that now. */
  1203. datap->size += (vma + size) - (datap->where + datap->size);
  1204. /* Update the section size. This happens only if we update the
  1205. 32-bit-aligned chunk size. Callers that have
  1206. non-32-bit-aligned sections should do all allocation and
  1207. size-setting by themselves or at least set the section size
  1208. after the last allocating call to this function. */
  1209. if (vma + size > sec->vma + sec->size)
  1210. sec->size += (vma + size) - (sec->vma + sec->size);
  1211. return datap->data + vma - datap->where;
  1212. }
  1213. }
  1214. /* Not found; allocate a new block. First check in case we get a
  1215. request for a size split up over several blocks; we'll have to return
  1216. NULL for those cases, requesting the caller to split up the request.
  1217. Requests with an address aligned on MMO_SEC_CONTENTS_CHUNK_SIZE bytes and
  1218. for no more than MMO_SEC_CONTENTS_CHUNK_SIZE will always get resolved. */
  1219. for (datap = sdatap->head; datap != NULL; datap = datap->next)
  1220. if ((datap->where <= vma && datap->where + datap->size > vma)
  1221. || (datap->where < vma + size
  1222. && datap->where + datap->size >= vma + size))
  1223. return NULL;
  1224. allocated_size
  1225. = (size + MMO_SEC_CONTENTS_CHUNK_SIZE - 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE - 1);
  1226. entry = (mmo_data_list_type *)
  1227. bfd_zalloc (sec->owner, sizeof (mmo_data_list_type) + allocated_size);
  1228. if (entry == NULL)
  1229. return NULL;
  1230. entry->where = vma;
  1231. entry->size = size;
  1232. entry->allocated_size = allocated_size;
  1233. datap = sdatap->head;
  1234. /* Sort the records by address. Optimize for the common case of adding
  1235. a record to the end of the list. */
  1236. if (sdatap->tail != NULL && entry->where >= sdatap->tail->where)
  1237. {
  1238. sdatap->tail->next = entry;
  1239. entry->next = NULL;
  1240. sdatap->tail = entry;
  1241. }
  1242. else
  1243. {
  1244. mmo_data_list_type **look;
  1245. for (look = &sdatap->head;
  1246. *look != NULL && (*look)->where < entry->where;
  1247. look = &(*look)->next)
  1248. ;
  1249. entry->next = *look;
  1250. *look = entry;
  1251. if (entry->next == NULL)
  1252. {
  1253. sdatap->tail = entry;
  1254. /* We get here for the first time (at other times too) for this
  1255. section. Say we have contents. */
  1256. if (! bfd_set_section_flags (sec->owner, sec,
  1257. bfd_get_section_flags (sec->owner, sec)
  1258. | SEC_HAS_CONTENTS))
  1259. return NULL;
  1260. }
  1261. }
  1262. /* Update the section size. This happens only when we add contents and
  1263. re-size as we go. The section size will then be aligned to 32 bits. */
  1264. if (vma + size > sec->vma + sec->size)
  1265. sec->size += (vma + size) - (sec->vma + sec->size);
  1266. return entry->data;
  1267. }
  1268. /* Set sizes once we've read in all sections. */
  1269. static void
  1270. mmo_map_set_sizes (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
  1271. void *ignored ATTRIBUTE_UNUSED)
  1272. {
  1273. sec->lma = sec->vma;
  1274. }
  1275. /* Read the mmo file and turn it into sections. */
  1276. static bfd_boolean
  1277. mmo_scan (bfd *abfd)
  1278. {
  1279. unsigned int i;
  1280. unsigned int lineno = 1;
  1281. bfd_boolean error = FALSE;
  1282. bfd_vma vma = 0;
  1283. asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
  1284. asection *non_spec_sec = NULL;
  1285. bfd_vma non_spec_vma = 0;
  1286. bfd_size_type nbytes_read = 0;
  1287. /* Buffer with room to read a 64-bit value. */
  1288. bfd_byte buf[8];
  1289. long stab_loc = -1;
  1290. char *file_names[256];
  1291. abfd->symcount = 0;
  1292. memset (file_names, 0, sizeof (file_names));
  1293. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  1294. goto error_return;
  1295. while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
  1296. {
  1297. if (buf[0] == LOP)
  1298. {
  1299. unsigned int y = bfd_get_8 (abfd, buf + 2);
  1300. unsigned int z = bfd_get_8 (abfd, buf + 3);
  1301. /* Change back to the original section for lopcodes other
  1302. than LOP_QUOTE that comes after a LOP_SPEC. */
  1303. if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
  1304. && non_spec_sec != NULL)
  1305. {
  1306. sec = non_spec_sec;
  1307. vma = non_spec_vma;
  1308. non_spec_sec = NULL;
  1309. }
  1310. switch (buf[1])
  1311. {
  1312. default:
  1313. (*_bfd_error_handler)
  1314. (_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
  1315. bfd_get_filename (abfd), buf[1]);
  1316. bfd_set_error (bfd_error_bad_value);
  1317. goto error_return;
  1318. case LOP_QUOTE:
  1319. /* Quote the next 32-bit word. */
  1320. if (y != 0 || z != 1)
  1321. {
  1322. (*_bfd_error_handler)
  1323. (_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
  1324. bfd_get_filename (abfd), y*256+z);
  1325. bfd_set_error (bfd_error_bad_value);
  1326. goto error_return;
  1327. }
  1328. if (bfd_bread (buf, 4, abfd) != 4)
  1329. goto error_return;
  1330. vma &= ~3;
  1331. mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
  1332. vma += 4;
  1333. lineno++;
  1334. break;
  1335. case LOP_LOC:
  1336. /* Set vma (and section). */
  1337. vma = (bfd_vma) y << 56;
  1338. if (z == 1)
  1339. {
  1340. /* Get a 32-bit value. */
  1341. if (bfd_bread (buf, 4, abfd) != 4)
  1342. goto error_return;
  1343. vma += bfd_get_32 (abfd, buf);
  1344. }
  1345. else if (z == 2)
  1346. {
  1347. /* Get a 64-bit value. */
  1348. if (bfd_bread (buf, 8, abfd) != 8)
  1349. goto error_return;
  1350. vma += bfd_get_64 (abfd, buf);
  1351. }
  1352. else
  1353. {
  1354. (*_bfd_error_handler)
  1355. (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
  1356. bfd_get_filename (abfd), z);
  1357. bfd_set_error (bfd_error_bad_value);
  1358. goto error_return;
  1359. }
  1360. /* When we decide which section the data goes into, we might
  1361. create the section. If that happens, make sure the VMA at
  1362. creation time is tetra-aligned. */
  1363. sec = mmo_decide_section (abfd, vma & ~3);
  1364. if (sec == NULL)
  1365. goto error_return;
  1366. break;
  1367. case LOP_SKIP:
  1368. /* Move forward within the same section. */
  1369. vma += y * 256 + z;
  1370. sec = mmo_decide_section (abfd, vma);
  1371. if (sec == NULL)
  1372. goto error_return;
  1373. break;
  1374. case LOP_FIXO:
  1375. /* A fixup: Store the current vma somewhere. Position using
  1376. same format as LOP_LOC. */
  1377. {
  1378. bfd_vma p = (bfd_vma) y << 56;
  1379. asection *fixosec;
  1380. if (z == 1)
  1381. {
  1382. /* Get a 32-bit value. */
  1383. if (bfd_bread (buf, 4, abfd) != 4)
  1384. goto error_return;
  1385. p += bfd_get_32 (abfd, buf);
  1386. }
  1387. else if (z == 2)
  1388. {
  1389. /* Get a 64-bit value. */
  1390. if (bfd_bread (buf, 8, abfd) != 8)
  1391. goto error_return;
  1392. p += bfd_get_64 (abfd, buf);
  1393. }
  1394. else
  1395. {
  1396. (*_bfd_error_handler)
  1397. (_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
  1398. bfd_get_filename (abfd), z);
  1399. bfd_set_error (bfd_error_bad_value);
  1400. goto error_return;
  1401. }
  1402. /* The section where we store this address might be a
  1403. different one than the current section. */
  1404. fixosec = mmo_decide_section (abfd, p);
  1405. if (fixosec == NULL)
  1406. goto error_return;
  1407. mmo_xore_64 (fixosec, p, vma);
  1408. }
  1409. break;
  1410. case LOP_FIXR:
  1411. /* A fixup: Store YZ of this lopcode into YZ at vma - 4 * yz. */
  1412. {
  1413. unsigned int yz = (y * 256 + z);
  1414. bfd_vma p = vma + 2 - 4 * yz;
  1415. asection *fixrsec = mmo_decide_section (abfd, p);
  1416. if (fixrsec == NULL)
  1417. goto error_return;
  1418. mmo_xore_16 (fixrsec, p, yz);
  1419. }
  1420. break;
  1421. case LOP_FIXRX:
  1422. /* A fixup, similar to lop_fixr, but taking larger numbers
  1423. and can change branches into the opposite direction
  1424. (gasp!). */
  1425. {
  1426. bfd_vma delta;
  1427. bfd_vma p;
  1428. asection *fixrsec;
  1429. if (y != 0)
  1430. {
  1431. (*_bfd_error_handler)
  1432. (_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
  1433. bfd_get_filename (abfd), y);
  1434. bfd_set_error (bfd_error_bad_value);
  1435. goto error_return;
  1436. }
  1437. if (z != 16 && z != 24)
  1438. {
  1439. (*_bfd_error_handler)
  1440. (_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
  1441. bfd_get_filename (abfd), z);
  1442. bfd_set_error (bfd_error_bad_value);
  1443. goto error_return;
  1444. }
  1445. /* Get the next 32-bit value. */
  1446. if (bfd_bread (buf, 4, abfd) != 4)
  1447. goto error_return;
  1448. delta = bfd_get_32 (abfd, buf);
  1449. /* Do an, ehm, involved calculation for the location of
  1450. the fixup. See mmixal documentation for a verbose
  1451. explanation. We follow it verbosely here for the
  1452. readers delight. */
  1453. if (buf[0] == 0)
  1454. p = vma - 4 * delta;
  1455. else if (buf[0] == 1)
  1456. p = vma - 4 * ((delta & 0xffffff) - (1 << z));
  1457. else
  1458. {
  1459. (*_bfd_error_handler)
  1460. (_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
  1461. bfd_get_filename (abfd), buf[0]);
  1462. bfd_set_error (bfd_error_bad_value);
  1463. goto error_return;
  1464. }
  1465. fixrsec = mmo_decide_section (abfd, vma);
  1466. if (fixrsec == NULL)
  1467. goto error_return;
  1468. mmo_xore_32 (fixrsec, p, delta);
  1469. }
  1470. break;
  1471. case LOP_FILE:
  1472. /* Set current file and perhaps the file name. Reset line
  1473. number. */
  1474. if (z != 0)
  1475. {
  1476. char *fname = bfd_malloc (z * 4 + 1);
  1477. if (fname == NULL)
  1478. {
  1479. (*_bfd_error_handler)
  1480. (_("%s: cannot allocate file name for file number %d, %d bytes\n"),
  1481. bfd_get_filename (abfd), y, z * 4 + 1);
  1482. bfd_set_error (bfd_error_system_call);
  1483. goto error_return;
  1484. }
  1485. fname[z * 4] = 0;
  1486. for (i = 0; i < z; i++)
  1487. {
  1488. if (bfd_bread (fname + i * 4, 4, abfd) != 4)
  1489. {
  1490. free (fname);
  1491. goto error_return;
  1492. }
  1493. }
  1494. if (file_names[y] != NULL)
  1495. {
  1496. (*_bfd_error_handler)
  1497. (_("%s: invalid mmo file: file number %d `%s',"
  1498. " was already entered as `%s'\n"),
  1499. bfd_get_filename (abfd), y, fname, file_names[y]);
  1500. bfd_set_error (bfd_error_bad_value);
  1501. goto error_return;
  1502. }
  1503. file_names[y] = fname;
  1504. }
  1505. if (file_names[y] == NULL)
  1506. {
  1507. (*_bfd_error_handler)
  1508. (_("%s: invalid mmo file: file name for number %d"
  1509. " was not specified before use\n"),
  1510. bfd_get_filename (abfd), y);
  1511. bfd_set_error (bfd_error_bad_value);
  1512. goto error_return;
  1513. }
  1514. lineno = 0;
  1515. break;
  1516. case LOP_LINE:
  1517. /* Set line number. */
  1518. lineno = y * 256 + z;
  1519. /* FIXME: Create a sequence of mmo-specific line number
  1520. entries for each section, then translate into canonical
  1521. format. */
  1522. break;
  1523. case LOP_SPEC:
  1524. /* Special data follows until the next non-lop_quote
  1525. lopcode. */
  1526. non_spec_sec = sec;
  1527. non_spec_vma = vma;
  1528. sec = mmo_get_spec_section (abfd, y * 256 + z);
  1529. if (sec == NULL)
  1530. goto error_return;
  1531. vma = sec->vma;
  1532. break;
  1533. case LOP_PRE:
  1534. {
  1535. /* We ignore header information, except we read in the
  1536. creation time from the first 32-bit word with the time
  1537. in seconds since era. */
  1538. if (z >= 1
  1539. && bfd_bread (abfd->tdata.mmo_data->created, 4,
  1540. abfd) != 4)
  1541. goto error_return;
  1542. for (i = 1; i < z; i++)
  1543. if (bfd_bread (buf, 4, abfd) != 4)
  1544. goto error_return;
  1545. }
  1546. break;
  1547. case LOP_POST:
  1548. /* This tells of the contents of registers $Z..$255 at
  1549. startup. We make a section out of it, with VMA = Z * 8,
  1550. but only if Z != 255 or the contents is non-zero. */
  1551. {
  1552. asection *rsec;
  1553. bfd_byte *loc;
  1554. bfd_vma first_octa;
  1555. bfd_vma startaddr_octa;
  1556. /* Read first octaword outside loop to simplify logic when
  1557. excluding the Z == 255, octa == 0 case. */
  1558. if (bfd_bread (buf, 8, abfd) != 8)
  1559. goto error_return;
  1560. first_octa = bfd_get_64 (abfd, buf);
  1561. /* Don't emit contents for the trivial case which is
  1562. always present; $255 pointing to Main. */
  1563. if (z != 255)
  1564. {
  1565. rsec
  1566. = bfd_make_section_old_way (abfd,
  1567. MMIX_REG_CONTENTS_SECTION_NAME);
  1568. rsec->flags |= SEC_LINKER_CREATED;
  1569. rsec->vma = z * 8;
  1570. loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
  1571. bfd_put_64 (abfd, first_octa, loc);
  1572. for (i = z + 1; i < 255; i++)
  1573. {
  1574. if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
  1575. goto error_return;
  1576. }
  1577. /* Read out the last octabyte, and use it to set the
  1578. start address. */
  1579. if (bfd_bread (buf, 8, abfd) != 8)
  1580. goto error_return;
  1581. startaddr_octa = bfd_get_64 (abfd, buf);
  1582. }
  1583. else
  1584. startaddr_octa = first_octa;
  1585. if (! bfd_set_start_address (abfd, startaddr_octa))
  1586. {
  1587. /* Currently this can't fail, but this should handle
  1588. future failures. */
  1589. bfd_set_error (bfd_error_bad_value);
  1590. goto error_return;
  1591. }
  1592. }
  1593. break;
  1594. case LOP_STAB:
  1595. /* We read in the symbols now, not later. */
  1596. if (y != 0 || z != 0)
  1597. {
  1598. (*_bfd_error_handler)
  1599. (_("%s: invalid mmo file: fields y and z of lop_stab"
  1600. " non-zero, y: %d, z: %d\n"),
  1601. bfd_get_filename (abfd), y, z);
  1602. bfd_set_error (bfd_error_bad_value);
  1603. goto error_return;
  1604. }
  1605. /* Save the location, so we can check that YZ in the LOP_END
  1606. is correct. */
  1607. stab_loc = bfd_tell (abfd);
  1608. /* It's not said that an MMO can be without symbols (though
  1609. mmixal will refuse to assemble files without Main), but
  1610. it seems it would still be a valid mmo-file, so allow it.
  1611. We detect the absence of a symbol area in that the upper
  1612. limit is computed (from the lop_end YZ field) as 0.
  1613. Don't call mmo_get_symbols; it can only detect the end of
  1614. a valid symbol trie, not the absence of one. */
  1615. if (abfd->tdata.mmo_data->max_symbol_length != 0
  1616. && ! mmo_get_symbols (abfd))
  1617. goto error_return;
  1618. break;
  1619. case LOP_END:
  1620. {
  1621. /* This must be the last 32-bit word in an mmo file.
  1622. Let's find out. */
  1623. struct stat statbuf;
  1624. file_ptr curpos = bfd_tell (abfd);
  1625. if (bfd_stat (abfd, &statbuf) < 0)
  1626. goto error_return;
  1627. if (statbuf.st_size != curpos)
  1628. {
  1629. (*_bfd_error_handler)
  1630. (_("%s: invalid mmo file: lop_end not last item in"
  1631. " file\n"),
  1632. bfd_get_filename (abfd));
  1633. bfd_set_error (bfd_error_bad_value);
  1634. goto error_return;
  1635. }
  1636. /* Check that the YZ field is right. Subtract the size of
  1637. this LOP_END in the calculation; YZ does not include
  1638. it. */
  1639. if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
  1640. {
  1641. (*_bfd_error_handler)
  1642. (_("%s: invalid mmo file: YZ of lop_end (%ld)"
  1643. " not equal to the number of tetras to the preceding"
  1644. " lop_stab (%ld)\n"),
  1645. bfd_get_filename (abfd), (long) (y * 256 + z),
  1646. (curpos - stab_loc - 4)/4);
  1647. bfd_set_error (bfd_error_bad_value);
  1648. goto error_return;
  1649. }
  1650. bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
  1651. goto done;
  1652. }
  1653. }
  1654. }
  1655. else
  1656. {
  1657. /* This wasn't a lopcode, so store it in the current section. */
  1658. mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
  1659. vma += 4;
  1660. vma &= ~3;
  1661. lineno++;
  1662. }
  1663. }
  1664. /* We know this file is a multiple of four bytes (checked in
  1665. mmo_object_p), so if we got something other than 0, this was a bad
  1666. file (although it's more likely we'll get 0 in that case too).
  1667. If we got end-of-file, then there was no lop_stab, so the file has
  1668. invalid format. */
  1669. if (nbytes_read != 0)
  1670. bfd_set_error (bfd_error_system_call);
  1671. else
  1672. bfd_set_error (bfd_error_bad_value);
  1673. error_return:
  1674. error = TRUE;
  1675. done:
  1676. /* Mark the .text and .data section with their normal attribute if they
  1677. contain anything. This is not redundant wrt. mmo_decide_section,
  1678. since that code might never execute, and conversely the alloc+code
  1679. section flags must be set then. */
  1680. sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
  1681. if (sec != NULL
  1682. && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
  1683. && ! bfd_set_section_flags (abfd, sec,
  1684. bfd_get_section_flags (abfd, sec)
  1685. | SEC_ALLOC | SEC_LOAD | SEC_CODE))
  1686. error = TRUE;
  1687. sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
  1688. if (sec != NULL
  1689. && (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
  1690. && ! bfd_set_section_flags (abfd, sec,
  1691. bfd_get_section_flags (abfd, sec)
  1692. | SEC_ALLOC | SEC_LOAD))
  1693. error = TRUE;
  1694. /* Free whatever resources we took. */
  1695. for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
  1696. if (file_names[i])
  1697. free (file_names[i]);
  1698. return ! error;
  1699. }
  1700. /* A hook to set up object file dependent section information. For mmo,
  1701. we point out the shape of allocated section contents. */
  1702. static bfd_boolean
  1703. mmo_new_section_hook (bfd *abfd, asection *newsect)
  1704. {
  1705. if (!newsect->used_by_bfd)
  1706. {
  1707. /* We zero-fill all fields and assume NULL is represented by an all
  1708. zero-bit pattern. */
  1709. newsect->used_by_bfd
  1710. = bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
  1711. if (!newsect->used_by_bfd)
  1712. return FALSE;
  1713. }
  1714. /* Always align to at least 32-bit words. */
  1715. newsect->alignment_power = 2;
  1716. return _bfd_generic_new_section_hook (abfd, newsect);
  1717. }
  1718. /* We already have section contents loaded for sections that have
  1719. contents. */
  1720. static bfd_boolean
  1721. mmo_get_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
  1722. asection *sec,
  1723. void * location,
  1724. file_ptr offset,
  1725. bfd_size_type bytes_to_do)
  1726. {
  1727. /* Iterate over diminishing chunk sizes, copying contents, like
  1728. mmo_set_section_contents. */
  1729. while (bytes_to_do)
  1730. {
  1731. /* A minor song-and-dance to make sure we're not bitten by the
  1732. distant possibility of the cast from bfd_vma to int making the
  1733. chunk zero-sized. */
  1734. int chunk_size
  1735. = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
  1736. bfd_byte *loc;
  1737. do
  1738. loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
  1739. while (loc == NULL && (chunk_size /= 2) != 0);
  1740. if (chunk_size == 0)
  1741. return FALSE;
  1742. memcpy (location, loc, chunk_size);
  1743. location += chunk_size;
  1744. bytes_to_do -= chunk_size;
  1745. offset += chunk_size;
  1746. }
  1747. return TRUE;
  1748. }
  1749. /* Return the amount of memory needed to read the symbol table. */
  1750. static long
  1751. mmo_get_symtab_upper_bound (bfd *abfd)
  1752. {
  1753. return (abfd->symcount + 1) * sizeof (asymbol *);
  1754. }
  1755. /* Sort mmo symbols by serial number. */
  1756. static int
  1757. mmo_sort_mmo_symbols (const void *arg1, const void *arg2)
  1758. {
  1759. const struct mmo_symbol *sym1 = *(const struct mmo_symbol **) arg1;
  1760. const struct mmo_symbol *sym2 = *(const struct mmo_symbol **) arg2;
  1761. /* Sort by serial number first. */
  1762. if (sym1->serno < sym2->serno)
  1763. return -1;
  1764. else if (sym1->serno > sym2->serno)
  1765. return 1;
  1766. /* Then sort by address of the table entries. */
  1767. return ((const char *) arg1 - (const char *) arg2);
  1768. }
  1769. /* Translate the symbol table. */
  1770. static long
  1771. mmo_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  1772. {
  1773. unsigned int symcount = bfd_get_symcount (abfd);
  1774. asymbol *csymbols;
  1775. unsigned int i;
  1776. csymbols = abfd->tdata.mmo_data->csymbols;
  1777. if (csymbols == NULL && symcount != 0)
  1778. {
  1779. asymbol *c;
  1780. struct mmo_symbol *s;
  1781. struct mmo_symbol **msp;
  1782. /* First we store the symbols into the table we'll return, then we
  1783. qsort it on the serial number, with secondary on the address of
  1784. the symbol, to preserve order if there would be non-unique serial
  1785. numbers. */
  1786. for (s = abfd->tdata.mmo_data->symbols,
  1787. msp = (struct mmo_symbol **) alocation;
  1788. s != NULL;
  1789. s = s->next, ++msp)
  1790. *msp = s;
  1791. *msp = NULL;
  1792. qsort (alocation, symcount, sizeof (struct mmo_symbol *),
  1793. mmo_sort_mmo_symbols);
  1794. csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
  1795. if (csymbols == NULL)
  1796. return -1;
  1797. abfd->tdata.mmo_data->csymbols = csymbols;
  1798. for (msp = (struct mmo_symbol **) alocation, c = csymbols;
  1799. *msp != NULL;
  1800. msp++, ++c)
  1801. {
  1802. s = *msp;
  1803. c->the_bfd = abfd;
  1804. c->name = s->name;
  1805. c->value = s->value;
  1806. c->flags = BSF_GLOBAL;
  1807. if (s->sym_type == mmo_data_sym)
  1808. {
  1809. c->section
  1810. = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
  1811. if (c->section == NULL)
  1812. c->section = bfd_abs_section_ptr;
  1813. else
  1814. c->value -= c->section->vma;
  1815. }
  1816. else if (s->sym_type == mmo_undef_sym)
  1817. c->section = bfd_und_section_ptr;
  1818. else if (s->sym_type == mmo_reg_sym)
  1819. {
  1820. c->section
  1821. = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
  1822. c->section->flags |= SEC_LINKER_CREATED;
  1823. }
  1824. else
  1825. {
  1826. asection *textsec
  1827. = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
  1828. asection *datasec;
  1829. if (textsec != NULL
  1830. && c->value >= textsec->vma
  1831. && c->value <= textsec->vma + textsec->size)
  1832. {
  1833. c->section = textsec;
  1834. c->value -= c->section->vma;
  1835. }
  1836. /* In mmo, symbol types depend on the VMA. Therefore, if
  1837. the data section isn't within the usual bounds, its
  1838. symbols are marked as absolute. Correct that. This
  1839. means we can't have absolute symbols with values matching
  1840. data section addresses, but we also can't have with
  1841. absolute symbols with values matching text section
  1842. addresses. For such needs, use the ELF format. */
  1843. else if ((datasec
  1844. = bfd_get_section_by_name (abfd,
  1845. MMO_DATA_SECTION_NAME))
  1846. != NULL
  1847. && c->value >= datasec->vma
  1848. && c->value <= datasec->vma + datasec->size)
  1849. {
  1850. c->section = datasec;
  1851. c->value -= c->section->vma;
  1852. }
  1853. else
  1854. c->section = bfd_abs_section_ptr;
  1855. }
  1856. c->udata.p = NULL;
  1857. }
  1858. }
  1859. /* Last, overwrite the incoming table with the right-type entries. */
  1860. for (i = 0; i < symcount; i++)
  1861. *alocation++ = csymbols++;
  1862. *alocation = NULL;
  1863. return symcount;
  1864. }
  1865. /* Get information about a symbol. */
  1866. static void
  1867. mmo_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  1868. asymbol *symbol, symbol_info *ret)
  1869. {
  1870. bfd_symbol_info (symbol, ret);
  1871. }
  1872. static void
  1873. mmo_print_symbol (bfd *abfd, void *afile, asymbol *symbol,
  1874. bfd_print_symbol_type how)
  1875. {
  1876. FILE *file = (FILE *) afile;
  1877. switch (how)
  1878. {
  1879. case bfd_print_symbol_name:
  1880. fprintf (file, "%s", symbol->name);
  1881. break;
  1882. default:
  1883. bfd_print_symbol_vandf (abfd, file, symbol);
  1884. fprintf (file, " %-5s %s",
  1885. symbol->section->name,
  1886. symbol->name);
  1887. }
  1888. }
  1889. /* We can't map a file directly into executable code, so the
  1890. size of header information is irrelevant. */
  1891. static int
  1892. mmo_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  1893. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  1894. {
  1895. return 0;
  1896. }
  1897. /* Write the (section-neutral) file preamble. */
  1898. static bfd_boolean
  1899. mmo_internal_write_header (bfd *abfd)
  1900. {
  1901. const char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
  1902. if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
  1903. return FALSE;
  1904. /* Copy creation time of original file. */
  1905. if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
  1906. return FALSE;
  1907. return TRUE;
  1908. }
  1909. /* Write the LOP_POST record, with global register initializations.
  1910. Z is the Z field of the LOP_POST, corresponding to 255 - number of
  1911. registers at DATA. The Z = 255 field is filled in with the
  1912. start-address. */
  1913. static bfd_boolean
  1914. mmo_internal_write_post (bfd *abfd, int z, asection *sec)
  1915. {
  1916. int i;
  1917. bfd_byte buf[8];
  1918. mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
  1919. for (i = z; i < 255; i++)
  1920. {
  1921. bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
  1922. if (bfd_bwrite (data, 8, abfd) != 8)
  1923. return FALSE;
  1924. }
  1925. /* For Z == $255, we always emit the start location; supposedly Main,
  1926. but we have it handy at bfd_get_start_address. If we're called with
  1927. Z == 255, don't assume DATA is valid. */
  1928. bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
  1929. return ! abfd->tdata.mmo_data->have_error && bfd_bwrite (buf, 8, abfd) == 8;
  1930. }
  1931. /* Translate to and from BFD flags. This is to make sure that we don't
  1932. get bitten by BFD flag number changes. */
  1933. static flagword
  1934. mmo_sec_flags_from_bfd_flags (flagword flags)
  1935. {
  1936. flagword oflags = 0;
  1937. if (flags & SEC_ALLOC)
  1938. oflags |= MMO_SEC_ALLOC;
  1939. if (flags & SEC_LOAD)
  1940. oflags |= MMO_SEC_LOAD;
  1941. if (flags & SEC_RELOC)
  1942. oflags |= MMO_SEC_RELOC;
  1943. if (flags & SEC_READONLY)
  1944. oflags |= MMO_SEC_READONLY;
  1945. if (flags & SEC_CODE)
  1946. oflags |= MMO_SEC_CODE;
  1947. if (flags & SEC_DATA)
  1948. oflags |= MMO_SEC_DATA;
  1949. if (flags & SEC_NEVER_LOAD)
  1950. oflags |= MMO_SEC_NEVER_LOAD;
  1951. if (flags & SEC_IS_COMMON)
  1952. oflags |= MMO_SEC_IS_COMMON;
  1953. if (flags & SEC_DEBUGGING)
  1954. oflags |= MMO_SEC_DEBUGGING;
  1955. return oflags;
  1956. }
  1957. static flagword
  1958. bfd_sec_flags_from_mmo_flags (flagword flags)
  1959. {
  1960. flagword oflags = 0;
  1961. if (flags & MMO_SEC_ALLOC)
  1962. oflags |= SEC_ALLOC;
  1963. if (flags & MMO_SEC_LOAD)
  1964. oflags |= SEC_LOAD;
  1965. if (flags & MMO_SEC_RELOC)
  1966. oflags |= SEC_RELOC;
  1967. if (flags & MMO_SEC_READONLY)
  1968. oflags |= SEC_READONLY;
  1969. if (flags & MMO_SEC_CODE)
  1970. oflags |= SEC_CODE;
  1971. if (flags & MMO_SEC_DATA)
  1972. oflags |= SEC_DATA;
  1973. if (flags & MMO_SEC_NEVER_LOAD)
  1974. oflags |= SEC_NEVER_LOAD;
  1975. if (flags & MMO_SEC_IS_COMMON)
  1976. oflags |= SEC_IS_COMMON;
  1977. if (flags & MMO_SEC_DEBUGGING)
  1978. oflags |= SEC_DEBUGGING;
  1979. return oflags;
  1980. }
  1981. /* Return TRUE iff the leading or trailing tetrabyte in SEC is defined and
  1982. is 0. */
  1983. static bfd_boolean
  1984. mmo_has_leading_or_trailing_zero_tetra_p (bfd *abfd, asection *sec)
  1985. {
  1986. bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
  1987. if (sec->size < 4)
  1988. return FALSE;
  1989. if (bfd_get_32 (abfd, mmo_get_loc (sec, secaddr, 4)) == 0
  1990. && bfd_get_32 (abfd,
  1991. mmo_get_loc (sec, secaddr + sec->size - 4, 4)) == 0)
  1992. return TRUE;
  1993. return FALSE;
  1994. }
  1995. /* Write a section. */
  1996. static bfd_boolean
  1997. mmo_internal_write_section (bfd *abfd, asection *sec)
  1998. {
  1999. /* We do it differently depending on what section this is:
  2000. ".text": Output, prepended by information about the first source file
  2001. (not yet implemented.)
  2002. ".data": Output.
  2003. (".MMIX.reg_contents": Not handled here.)
  2004. Anything else: Output inside a lop_spec 80, in the format described
  2005. above. */
  2006. if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
  2007. {
  2008. bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
  2009. /* Because leading and trailing zeros are omitted in output, we need to
  2010. specify the section boundaries so they're correct when the file
  2011. is read in again. That's also the case if this section is
  2012. specified as not within its usual boundaries or alignments. */
  2013. if (sec->size != 0
  2014. && (secaddr + sec->size >= (bfd_vma) 1 << 56
  2015. || (secaddr & 3) != 0
  2016. || (sec->size & 3) != 0
  2017. || mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
  2018. {
  2019. if (!mmo_write_section_description (abfd, sec))
  2020. return FALSE;
  2021. }
  2022. /* FIXME: Output source file name and line number. */
  2023. return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
  2024. }
  2025. else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
  2026. {
  2027. bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
  2028. /* Same goes as for MMO_TEXT_SECTION_NAME above. */
  2029. if (sec->size != 0
  2030. && (secaddr < (bfd_vma) 0x20 << 56
  2031. || secaddr + sec->size >= (bfd_vma) 0x21 << 56
  2032. || (secaddr & 3) != 0
  2033. || (sec->size & 3) != 0
  2034. || mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
  2035. {
  2036. if (!mmo_write_section_description (abfd, sec))
  2037. return FALSE;
  2038. }
  2039. return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
  2040. }
  2041. else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
  2042. /* Not handled here. */
  2043. {
  2044. /* This would normally be an abort call since this can't happen, but
  2045. we don't do that. */
  2046. bfd_set_error (bfd_error_bad_value);
  2047. return FALSE;
  2048. }
  2049. else if (CONST_STRNEQ (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX))
  2050. {
  2051. int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
  2052. mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
  2053. return (! abfd->tdata.mmo_data->have_error
  2054. && mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
  2055. }
  2056. /* Ignore sections that are just allocated or empty; we write out
  2057. _contents_ here. */
  2058. else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
  2059. && sec->size != 0)
  2060. {
  2061. if (!mmo_write_section_description (abfd, sec))
  2062. return FALSE;
  2063. /* Writing a LOP_LOC ends the LOP_SPEC data, and makes data actually
  2064. loaded. */
  2065. if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
  2066. return (! abfd->tdata.mmo_data->have_error
  2067. && mmo_write_loc_chunk_list (abfd,
  2068. mmo_section_data (sec)->head));
  2069. return (! abfd->tdata.mmo_data->have_error
  2070. && mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
  2071. }
  2072. /* Some section without contents. */
  2073. return TRUE;
  2074. }
  2075. /* Write the description of a section, extended-mmo-style. */
  2076. static bfd_boolean
  2077. mmo_write_section_description (bfd *abfd, asection *sec)
  2078. {
  2079. /* Keep the following document-comment formatted the way it is. */
  2080. /*
  2081. INODE
  2082. mmo section mapping, , Symbol-table, mmo
  2083. SUBSECTION
  2084. mmo section mapping
  2085. The implementation in BFD uses special data type 80 (decimal) to
  2086. encapsulate and describe named sections, containing e.g.@: debug
  2087. information. If needed, any datum in the encapsulation will be
  2088. quoted using lop_quote. First comes a 32-bit word holding the
  2089. number of 32-bit words containing the zero-terminated zero-padded
  2090. segment name. After the name there's a 32-bit word holding flags
  2091. describing the section type. Then comes a 64-bit big-endian word
  2092. with the section length (in bytes), then another with the section
  2093. start address. Depending on the type of section, the contents
  2094. might follow, zero-padded to 32-bit boundary. For a loadable
  2095. section (such as data or code), the contents might follow at some
  2096. later point, not necessarily immediately, as a lop_loc with the
  2097. same start address as in the section description, followed by the
  2098. contents. This in effect forms a descriptor that must be emitted
  2099. before the actual contents. Sections described this way must not
  2100. overlap.
  2101. For areas that don't have such descriptors, synthetic sections are
  2102. formed by BFD. Consecutive contents in the two memory areas
  2103. @samp{0x0000@dots{}00} to @samp{0x01ff@dots{}ff} and
  2104. @samp{0x2000@dots{}00} to @samp{0x20ff@dots{}ff} are entered in
  2105. sections named <<.text>> and <<.data>> respectively. If an area
  2106. is not otherwise described, but would together with a neighboring
  2107. lower area be less than @samp{0x40000000} bytes long, it is joined
  2108. with the lower area and the gap is zero-filled. For other cases,
  2109. a new section is formed, named <<.MMIX.sec.@var{n}>>. Here,
  2110. @var{n} is a number, a running count through the mmo file,
  2111. starting at 0.
  2112. EXAMPLE
  2113. A loadable section specified as:
  2114. | .section secname,"ax"
  2115. | TETRA 1,2,3,4,-1,-2009
  2116. | BYTE 80
  2117. and linked to address @samp{0x4}, is represented by the sequence:
  2118. | 0x98080050 - lop_spec 80
  2119. | 0x00000002 - two 32-bit words for the section name
  2120. | 0x7365636e - "secn"
  2121. | 0x616d6500 - "ame\0"
  2122. | 0x00000033 - flags CODE, READONLY, LOAD, ALLOC
  2123. | 0x00000000 - high 32 bits of section length
  2124. | 0x0000001c - section length is 28 bytes; 6 * 4 + 1 + alignment to 32 bits
  2125. | 0x00000000 - high 32 bits of section address
  2126. | 0x00000004 - section address is 4
  2127. | 0x98010002 - 64 bits with address of following data
  2128. | 0x00000000 - high 32 bits of address
  2129. | 0x00000004 - low 32 bits: data starts at address 4
  2130. | 0x00000001 - 1
  2131. | 0x00000002 - 2
  2132. | 0x00000003 - 3
  2133. | 0x00000004 - 4
  2134. | 0xffffffff - -1
  2135. | 0xfffff827 - -2009
  2136. | 0x50000000 - 80 as a byte, padded with zeros.
  2137. Note that the lop_spec wrapping does not include the section
  2138. contents. Compare this to a non-loaded section specified as:
  2139. | .section thirdsec
  2140. | TETRA 200001,100002
  2141. | BYTE 38,40
  2142. This, when linked to address @samp{0x200000000000001c}, is
  2143. represented by:
  2144. | 0x98080050 - lop_spec 80
  2145. | 0x00000002 - two 32-bit words for the section name
  2146. | 0x7365636e - "thir"
  2147. | 0x616d6500 - "dsec"
  2148. | 0x00000010 - flag READONLY
  2149. | 0x00000000 - high 32 bits of section length
  2150. | 0x0000000c - section length is 12 bytes; 2 * 4 + 2 + alignment to 32 bits
  2151. | 0x20000000 - high 32 bits of address
  2152. | 0x0000001c - low 32 bits of address 0x200000000000001c
  2153. | 0x00030d41 - 200001
  2154. | 0x000186a2 - 100002
  2155. | 0x26280000 - 38, 40 as bytes, padded with zeros
  2156. For the latter example, the section contents must not be
  2157. loaded in memory, and is therefore specified as part of the
  2158. special data. The address is usually unimportant but might
  2159. provide information for e.g.@: the DWARF 2 debugging format. */
  2160. mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
  2161. mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
  2162. mmo_write_chunk (abfd, (bfd_byte *) sec->name, strlen (sec->name));
  2163. mmo_flush_chunk (abfd);
  2164. /* FIXME: We can get debug sections (.debug_line & Co.) with a section
  2165. flag still having SEC_RELOC set. Investigate. This might be true
  2166. for all alien sections; perhaps mmo.em should clear that flag. Might
  2167. be related to weak references. */
  2168. mmo_write_tetra (abfd,
  2169. mmo_sec_flags_from_bfd_flags
  2170. (bfd_get_section_flags (abfd, sec)));
  2171. mmo_write_octa (abfd, sec->size);
  2172. mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
  2173. return TRUE;
  2174. }
  2175. /* We save up all data before output. */
  2176. static bfd_boolean
  2177. mmo_set_section_contents (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
  2178. const void *location, file_ptr offset,
  2179. bfd_size_type bytes_to_do)
  2180. {
  2181. /* Iterate over diminishing chunk sizes, copying contents. */
  2182. while (bytes_to_do)
  2183. {
  2184. /* A minor song-and-dance to make sure we're not bitten by the
  2185. distant possibility of the cast from bfd_vma to int making the
  2186. chunk zero-sized. */
  2187. int chunk_size
  2188. = (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
  2189. bfd_byte *loc;
  2190. do
  2191. loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
  2192. while (loc == NULL && (chunk_size /= 2) != 0);
  2193. if (chunk_size == 0)
  2194. return FALSE;
  2195. memcpy (loc, location, chunk_size);
  2196. location += chunk_size;
  2197. bytes_to_do -= chunk_size;
  2198. offset += chunk_size;
  2199. }
  2200. return TRUE;
  2201. }
  2202. /* Add a symbol to a trie-tree. */
  2203. static bfd_boolean
  2204. mmo_internal_add_3_sym (bfd *abfd, struct mmo_symbol_trie *rootp,
  2205. const struct mmo_symbol *symp)
  2206. {
  2207. const char *name = symp->name;
  2208. struct mmo_symbol_trie *trie = rootp;
  2209. struct mmo_symbol_trie **triep = NULL;
  2210. while (*name && trie != NULL)
  2211. {
  2212. if (*name < trie->symchar)
  2213. {
  2214. triep = &trie->left;
  2215. trie = trie->left;
  2216. }
  2217. else if (*name > trie->symchar)
  2218. {
  2219. triep = &trie->right;
  2220. trie = trie->right;
  2221. }
  2222. else if (*name == trie->symchar)
  2223. {
  2224. triep = &trie->middle;
  2225. name++;
  2226. /* Make sure "trie" points to where we should fill in the
  2227. current symbol whenever we've iterated through "name". We
  2228. would lose the right position if we encounter "foobar" then
  2229. "foo". */
  2230. if (*name)
  2231. trie = trie->middle;
  2232. }
  2233. }
  2234. while (*name != 0)
  2235. {
  2236. /* Create middle branches for the rest of the characters. */
  2237. trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
  2238. *triep = trie;
  2239. trie->symchar = *name++;
  2240. triep = &trie->middle;
  2241. }
  2242. /* We discover a duplicate symbol rather late in the process, but still;
  2243. we discover it and bail out. */
  2244. if (trie->sym.name != NULL)
  2245. {
  2246. (*_bfd_error_handler)
  2247. (_("%s: invalid symbol table: duplicate symbol `%s'\n"),
  2248. bfd_get_filename (abfd), trie->sym.name);
  2249. bfd_set_error (bfd_error_bad_value);
  2250. return FALSE;
  2251. }
  2252. memcpy (&trie->sym, symp, sizeof *symp);
  2253. return TRUE;
  2254. }
  2255. /* Find out the length of the serialized version of a trie in bytes. */
  2256. static unsigned int
  2257. mmo_internal_3_length (bfd *abfd, struct mmo_symbol_trie *trie)
  2258. {
  2259. /* First, one for the control byte. */
  2260. unsigned int length = 1;
  2261. if (trie == NULL)
  2262. return 0;
  2263. /* Add in the recursion to the left. */
  2264. length += mmo_internal_3_length (abfd, trie->left);
  2265. /* Add in the middle trie and the character. */
  2266. length += 1 + mmo_internal_3_length (abfd, trie->middle);
  2267. /* Add in the recursion to the right. */
  2268. length += mmo_internal_3_length (abfd, trie->right);
  2269. /* Add in bytes for the symbol (if this is an endnode). */
  2270. if (trie->sym.name != NULL)
  2271. {
  2272. unsigned int serno = trie->sym.serno;
  2273. /* First what it takes to encode the value. */
  2274. if (trie->sym.sym_type == mmo_reg_sym)
  2275. length++;
  2276. else if (trie->sym.sym_type == mmo_undef_sym)
  2277. length += 2;
  2278. else
  2279. {
  2280. bfd_vma value = trie->sym.value;
  2281. /* Coded in one to eight following bytes. */
  2282. if (trie->sym.sym_type == mmo_data_sym)
  2283. value -= (bfd_vma) 0x20 << 56;
  2284. do
  2285. {
  2286. value >>= 8;
  2287. length++;
  2288. }
  2289. while (value != 0);
  2290. }
  2291. /* Find out what it takes to encode the serial number. */
  2292. do
  2293. {
  2294. serno >>= 7;
  2295. length++;
  2296. }
  2297. while (serno != 0);
  2298. }
  2299. return length;
  2300. }
  2301. /* Helper function for outputting the serial number of a symbol, output as
  2302. a variant of leb128 (see dwarf2 documentation) which could be called
  2303. beb128. Using a helper function and recursion simplifies debugging. */
  2304. static void
  2305. mmo_beb128_out (bfd *abfd, int serno, int marker)
  2306. {
  2307. if (serno & ~0x7f)
  2308. mmo_beb128_out (abfd, serno >> 7, 0);
  2309. mmo_write_byte (abfd, marker | (serno & 0x7f));
  2310. }
  2311. /* Serialize a trie. */
  2312. static void
  2313. mmo_internal_3_dump (bfd *abfd, struct mmo_symbol_trie *trie)
  2314. {
  2315. bfd_byte control = 0;
  2316. if (trie == NULL)
  2317. return;
  2318. if (trie->left)
  2319. control |= MMO3_LEFT;
  2320. if (trie->middle)
  2321. control |= MMO3_MIDDLE;
  2322. if (trie->right)
  2323. control |= MMO3_RIGHT;
  2324. if (trie->sym.name != NULL)
  2325. {
  2326. /* Encode the symbol type and length of value bytes. */
  2327. if (trie->sym.sym_type == mmo_reg_sym)
  2328. control |= MMO3_REGQUAL_BITS;
  2329. else if (trie->sym.sym_type == mmo_undef_sym)
  2330. control |= MMO3_UNDEF;
  2331. else
  2332. {
  2333. bfd_vma value = trie->sym.value;
  2334. /* Coded in 1..8 following bytes. */
  2335. if (trie->sym.sym_type == mmo_data_sym)
  2336. {
  2337. control |= MMO3_DATA;
  2338. value -= (bfd_vma) 0x20 << 56;
  2339. }
  2340. do
  2341. {
  2342. value >>= 8;
  2343. control++;
  2344. }
  2345. while (value != 0);
  2346. }
  2347. }
  2348. /* The control byte is output before recursing. */
  2349. mmo_write_byte (abfd, control);
  2350. mmo_internal_3_dump (abfd, trie->left);
  2351. if (control & MMO3_SYMBITS)
  2352. {
  2353. mmo_write_byte (abfd, trie->symchar);
  2354. if (trie->sym.name != NULL)
  2355. {
  2356. if (trie->sym.sym_type == mmo_reg_sym)
  2357. mmo_write_byte (abfd, trie->sym.value);
  2358. else if (trie->sym.sym_type == mmo_undef_sym)
  2359. {
  2360. mmo_write_byte (abfd, 0);
  2361. mmo_write_byte (abfd, 0);
  2362. }
  2363. else
  2364. {
  2365. bfd_vma value = trie->sym.value;
  2366. bfd_byte byte_n = control & 15;
  2367. /* Coded in 1..8 following bytes. Note that the value is
  2368. shifted out big-endian. */
  2369. if (trie->sym.sym_type == mmo_data_sym)
  2370. {
  2371. value -= (bfd_vma) 0x20 << 56;
  2372. byte_n -= 8;
  2373. }
  2374. do
  2375. {
  2376. mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
  2377. byte_n--;
  2378. }
  2379. while (byte_n != 0);
  2380. }
  2381. mmo_beb128_out (abfd, trie->sym.serno, 128);
  2382. }
  2383. mmo_internal_3_dump (abfd, trie->middle);
  2384. }
  2385. mmo_internal_3_dump (abfd, trie->right);
  2386. }
  2387. /* Write symbols in mmo format. Also write the lop_end terminator. */
  2388. static bfd_boolean
  2389. mmo_write_symbols_and_terminator (bfd *abfd)
  2390. {
  2391. int count = bfd_get_symcount (abfd);
  2392. asymbol **table;
  2393. asymbol **orig_table = bfd_get_outsymbols (abfd);
  2394. int serno;
  2395. struct mmo_symbol_trie root;
  2396. int trie_len;
  2397. int i;
  2398. bfd_byte buf[4];
  2399. /* Create a symbol for "Main". */
  2400. asymbol *fakemain = bfd_make_empty_symbol (abfd);
  2401. fakemain->flags = BSF_GLOBAL;
  2402. fakemain->value = bfd_get_start_address (abfd);
  2403. fakemain->name = MMIX_START_SYMBOL_NAME;
  2404. fakemain->section = bfd_abs_section_ptr;
  2405. memset (&root, 0, sizeof (root));
  2406. /* Make all symbols take a left turn. */
  2407. root.symchar = 0xff;
  2408. /* There must always be a ":Main", so we'll add one if there are no
  2409. symbols. Make sure we have room for it. */
  2410. table = bfd_alloc (abfd, (count + 1) * sizeof (asymbol *));
  2411. if (table == NULL)
  2412. return FALSE;
  2413. memcpy (table, orig_table, count * sizeof (asymbol *));
  2414. /* Move :Main (if there is one) to the first position. This is
  2415. necessary to get the same layout of the trie-tree when linking as
  2416. when objcopying the result as in the objcopy.exp test "simple objcopy
  2417. of executable". It also automatically takes care of assigning serial
  2418. number 1 to :Main (as is mandatory). */
  2419. for (i = 0; i < count; i++)
  2420. if (table[i] != NULL
  2421. && strcmp (table[i]->name, MMIX_START_SYMBOL_NAME) == 0
  2422. && (table[i]->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL)
  2423. {
  2424. asymbol *mainsym = table[i];
  2425. bfd_vma mainvalue
  2426. = (mainsym->value
  2427. + mainsym->section->output_section->vma
  2428. + mainsym->section->output_offset);
  2429. memcpy (table + 1, orig_table, i * sizeof (asymbol *));
  2430. table[0] = mainsym;
  2431. /* Check that the value assigned to :Main is the same as the entry
  2432. address. The default linker script asserts this. This is as
  2433. good a place as any to check this consistency. */
  2434. if (mainvalue != bfd_get_start_address (abfd))
  2435. {
  2436. /* Arbitrary buffer to hold the printable representation of a
  2437. vma. */
  2438. char vmas_main[40];
  2439. char vmas_start[40];
  2440. bfd_vma vma_start = bfd_get_start_address (abfd);
  2441. sprintf_vma (vmas_main, mainvalue);
  2442. sprintf_vma (vmas_start, vma_start);
  2443. (*_bfd_error_handler)
  2444. (_("%s: Bad symbol definition: `Main' set to %s rather"
  2445. " than the start address %s\n"),
  2446. bfd_get_filename (abfd), vmas_main, vmas_start);
  2447. bfd_set_error (bfd_error_bad_value);
  2448. return FALSE;
  2449. }
  2450. break;
  2451. }
  2452. if (i == count && count != 0)
  2453. {
  2454. /* When there are symbols, there must be a :Main. There was no
  2455. :Main, so we need to add it manually. */
  2456. memcpy (table + 1, orig_table, count * sizeof (asymbol *));
  2457. table[0] = fakemain;
  2458. count++;
  2459. }
  2460. /* Don't bother inspecting symbols in plugin dummy objects; their
  2461. symbols aren't fully inspectable. */
  2462. if ((abfd->flags & BFD_PLUGIN) == 0)
  2463. {
  2464. for (i = 0, serno = 1; i < count && table[i] != NULL; i++)
  2465. {
  2466. asymbol *s = table[i];
  2467. /* It's not enough to consult bfd_is_local_label, since it does not
  2468. mean "local" in the sense of linkable-and-observable-after-link.
  2469. Let's just check the BSF_GLOBAL flag.
  2470. Also, don't export symbols with characters not in the
  2471. allowed set. */
  2472. if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
  2473. && strspn (s->name,
  2474. valid_mmo_symbol_character_set) == strlen (s->name))
  2475. {
  2476. struct mmo_symbol sym;
  2477. memset (&sym, 0, sizeof (sym));
  2478. /* Need to strip const here; strdup:ing would leak and the
  2479. existing string must be safe to reuse. */
  2480. sym.name = (char *) s->name;
  2481. sym.value =
  2482. s->value
  2483. + s->section->output_section->vma
  2484. + s->section->output_offset;
  2485. if (bfd_is_und_section (s->section))
  2486. sym.sym_type = mmo_undef_sym;
  2487. else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
  2488. /* The encoding of data symbols require that the "rest"
  2489. of the value fits in 6 bytes, so the upper two bytes
  2490. must be 0x2000. All other symbols get to be the
  2491. absolute type. */
  2492. && (sym.value >> 48) == 0x2000)
  2493. sym.sym_type = mmo_data_sym;
  2494. else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
  2495. sym.sym_type = mmo_reg_sym;
  2496. else if (strcmp (s->section->name,
  2497. MMIX_REG_CONTENTS_SECTION_NAME) == 0)
  2498. {
  2499. sym.sym_type = mmo_reg_sym;
  2500. sym.value /= 8;
  2501. }
  2502. else
  2503. sym.sym_type = mmo_abs_sym;
  2504. /* FIXME: We assume the order of the received symbols is an
  2505. ordered mapping of the serial numbers. This is not
  2506. necessarily true if we e.g. objcopy a mmo file to another and
  2507. there are gaps in the numbering. Not sure if this can
  2508. happen. Not sure what to do. */
  2509. sym.serno = serno++;
  2510. if (! mmo_internal_add_3_sym (abfd, &root, &sym))
  2511. return FALSE;
  2512. }
  2513. }
  2514. }
  2515. /* Change the root node to be a ":"-prefix. */
  2516. root.symchar = ':';
  2517. root.middle = root.left;
  2518. root.right = NULL;
  2519. root.left = NULL;
  2520. /* We have to find out if we can fit the whole symbol table in the mmo
  2521. symtab. It would be bad to assume we can always fit it in 262144
  2522. bytes. If we can't, just leave the Main symbol. */
  2523. trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
  2524. if (trie_len > 0xffff)
  2525. {
  2526. /* Test this code by using a lower limit in the test above and check
  2527. that the single "Main" symbol is emitted and handled properly.
  2528. There's no specific test-case. */
  2529. struct mmo_symbol sym;
  2530. (*_bfd_error_handler)
  2531. (_("%s: warning: symbol table too large for mmo, larger than 65535"
  2532. " 32-bit words: %d. Only `Main' will be emitted.\n"),
  2533. bfd_get_filename (abfd), trie_len);
  2534. memset (&sym, 0, sizeof (sym));
  2535. sym.sym_type = mmo_abs_sym;
  2536. sym.name = MMIX_START_SYMBOL_NAME;
  2537. sym.serno = 1;
  2538. sym.value = bfd_get_start_address (abfd);
  2539. /* Then patch up a symbol table to be just the ":Main" symbol. */
  2540. memset (&root, 0, sizeof (root));
  2541. root.left = root.middle;
  2542. root.symchar = 0xff;
  2543. root.middle = NULL;
  2544. root.right = NULL;
  2545. if (! mmo_internal_add_3_sym (abfd, &root, &sym))
  2546. return FALSE;
  2547. root.symchar = ':';
  2548. root.middle = root.left;
  2549. root.right = NULL;
  2550. root.left = NULL;
  2551. trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
  2552. }
  2553. /* Reset the written-bytes counter. */
  2554. abfd->tdata.mmo_data->byte_no = 0;
  2555. /* Put out the lop_stab mark. */
  2556. bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
  2557. if (bfd_bwrite (buf, 4, abfd) != 4)
  2558. return FALSE;
  2559. /* Dump out symbols. */
  2560. mmo_internal_3_dump (abfd, &root);
  2561. if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
  2562. {
  2563. /* I haven't seen this trig. It seems no use claiming this case
  2564. isn't debugged and abort if we get here. Instead emit a
  2565. diagnostic and fail "normally". */
  2566. (*_bfd_error_handler)
  2567. (_("%s: internal error, symbol table changed size from %d to %d"
  2568. " words\n"),
  2569. bfd_get_filename (abfd), trie_len,
  2570. (abfd->tdata.mmo_data->byte_no + 3)/4);
  2571. bfd_set_error (bfd_error_bad_value);
  2572. return FALSE;
  2573. }
  2574. /* Dump out remaining bytes in the buffer and handle I/O errors by
  2575. propagating errors. */
  2576. if ((abfd->tdata.mmo_data->byte_no % 4) != 0
  2577. || abfd->tdata.mmo_data->have_error)
  2578. {
  2579. memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
  2580. 0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
  2581. if (abfd->tdata.mmo_data->have_error
  2582. || bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
  2583. return FALSE;
  2584. }
  2585. bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
  2586. return bfd_bwrite (buf, 4, abfd) == 4;
  2587. }
  2588. /* Write section unless it is the register contents section. For that, we
  2589. instead store the section in the supplied pointer. This function is
  2590. used through bfd_map_over_sections. */
  2591. static void
  2592. mmo_write_section_unless_reg_contents (bfd *abfd, asection *sec, void *p)
  2593. {
  2594. struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
  2595. if (! infop->retval)
  2596. return;
  2597. if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
  2598. {
  2599. infop->reg_section = sec;
  2600. return;
  2601. }
  2602. /* Exclude the convenience register section. */
  2603. if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
  2604. {
  2605. if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
  2606. {
  2607. /* Make sure it hasn't got contents. It seems impossible to
  2608. make it carry contents, so we don't have a test-case for
  2609. this. */
  2610. (*_bfd_error_handler)
  2611. (_("%s: internal error, internal register section %s had"
  2612. " contents\n"),
  2613. bfd_get_filename (abfd), sec->name);
  2614. bfd_set_error (bfd_error_bad_value);
  2615. infop->retval = FALSE;
  2616. return;
  2617. }
  2618. return;
  2619. }
  2620. infop->retval = mmo_internal_write_section (abfd, sec);
  2621. }
  2622. /* Do the actual output of a file. Assumes mmo_set_section_contents is
  2623. already called. */
  2624. static bfd_boolean
  2625. mmo_write_object_contents (bfd *abfd)
  2626. {
  2627. struct mmo_write_sec_info wsecinfo;
  2628. /* First, there are a few words of preamble. */
  2629. if (! mmo_internal_write_header (abfd))
  2630. return FALSE;
  2631. wsecinfo.reg_section = NULL;
  2632. wsecinfo.retval = TRUE;
  2633. bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
  2634. &wsecinfo);
  2635. if (! wsecinfo.retval)
  2636. return FALSE;
  2637. if (wsecinfo.reg_section != NULL)
  2638. {
  2639. asection *sec = wsecinfo.reg_section;
  2640. unsigned int z = (unsigned int) (sec->vma / 8);
  2641. /* Registers 0..31 must not be global. Do sanity check on the "vma"
  2642. of the register contents section and check that it corresponds to
  2643. the length of the section. */
  2644. if (z < 32 || z >= 255 || (sec->vma & 7) != 0
  2645. || sec->vma != 256 * 8 - sec->size - 8)
  2646. {
  2647. bfd_set_error (bfd_error_bad_value);
  2648. if (sec->size == 0)
  2649. /* There must always be at least one such register. */
  2650. (*_bfd_error_handler)
  2651. (_("%s: no initialized registers; section length 0\n"),
  2652. bfd_get_filename (abfd));
  2653. else if (sec->vma > (256 - 32) * 8)
  2654. /* Provide better error message for the case of too many
  2655. global registers. */
  2656. (*_bfd_error_handler)
  2657. (_("%s: too many initialized registers; section length %ld\n"),
  2658. bfd_get_filename (abfd),
  2659. (long) sec->size);
  2660. else
  2661. (*_bfd_error_handler)
  2662. (_("%s: invalid start address for initialized registers of"
  2663. " length %ld: 0x%lx%08lx\n"),
  2664. bfd_get_filename (abfd),
  2665. (long) sec->size,
  2666. (unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
  2667. return FALSE;
  2668. }
  2669. if (! mmo_internal_write_post (abfd, z, sec))
  2670. return FALSE;
  2671. }
  2672. else
  2673. if (! mmo_internal_write_post (abfd, 255, NULL))
  2674. return FALSE;
  2675. return mmo_write_symbols_and_terminator (abfd);
  2676. }
  2677. /* If there's anything in particular in a mmo bfd that we want to free,
  2678. make this a real function. Only do this if you see major memory
  2679. thrashing; zealous free:ing will cause unwanted behavior, especially if
  2680. you "free" memory allocated with "bfd_alloc", or even "bfd_release" a
  2681. block allocated with "bfd_alloc"; they're really allocated from an
  2682. obstack, and we don't know what was allocated there since this
  2683. particular allocation. */
  2684. #define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
  2685. #define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  2686. /* Perhaps we need to adjust this one; mmo labels (originally) without a
  2687. leading ':' might more appropriately be called local. */
  2688. #define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
  2689. #define mmo_bfd_is_target_special_symbol \
  2690. ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  2691. #define mmo_get_symbol_version_string \
  2692. _bfd_nosymbols_get_symbol_version_string
  2693. /* Is this one really used or defined by anyone? */
  2694. #define mmo_get_lineno _bfd_nosymbols_get_lineno
  2695. /* FIXME: We can do better on this one, if we have a dwarf2 .debug_line
  2696. section or if MMO line numbers are implemented. */
  2697. #define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
  2698. #define mmo_find_line _bfd_nosymbols_find_line
  2699. #define mmo_find_inliner_info _bfd_nosymbols_find_inliner_info
  2700. #define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
  2701. #define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  2702. #define mmo_read_minisymbols _bfd_generic_read_minisymbols
  2703. #define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  2704. #define mmo_get_section_contents_in_window \
  2705. _bfd_generic_get_section_contents_in_window
  2706. #define mmo_bfd_get_relocated_section_contents \
  2707. bfd_generic_get_relocated_section_contents
  2708. #define mmo_bfd_gc_sections bfd_generic_gc_sections
  2709. #define mmo_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  2710. #define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  2711. #define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
  2712. #define mmo_bfd_link_just_syms _bfd_generic_link_just_syms
  2713. #define mmo_bfd_copy_link_hash_symbol_type \
  2714. _bfd_generic_copy_link_hash_symbol_type
  2715. #define mmo_bfd_final_link _bfd_generic_final_link
  2716. #define mmo_bfd_link_split_section _bfd_generic_link_split_section
  2717. /* Strictly speaking, only MMIX uses this restricted format, but let's not
  2718. stop anybody from shooting themselves in the foot. */
  2719. #define mmo_set_arch_mach bfd_default_set_arch_mach
  2720. #define mmo_bfd_relax_section bfd_generic_relax_section
  2721. #define mmo_bfd_merge_sections bfd_generic_merge_sections
  2722. #define mmo_bfd_is_group_section bfd_generic_is_group_section
  2723. #define mmo_bfd_discard_group bfd_generic_discard_group
  2724. #define mmo_section_already_linked \
  2725. _bfd_generic_section_already_linked
  2726. #define mmo_bfd_define_common_symbol bfd_generic_define_common_symbol
  2727. /* We want to copy time of creation, otherwise we'd use
  2728. BFD_JUMP_TABLE_COPY (_bfd_generic). */
  2729. #define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
  2730. #define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
  2731. #define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
  2732. #define mmo_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
  2733. #define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
  2734. #define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
  2735. const bfd_target mmix_mmo_vec =
  2736. {
  2737. "mmo", /* name */
  2738. bfd_target_mmo_flavour,
  2739. BFD_ENDIAN_BIG, /* target byte order */
  2740. BFD_ENDIAN_BIG, /* target headers byte order */
  2741. /* FIXME: Might need adjustments. */
  2742. (HAS_RELOC | EXEC_P | /* object flags */
  2743. HAS_LINENO | HAS_DEBUG |
  2744. HAS_SYMS | HAS_LOCALS | WP_TEXT),
  2745. /* FIXME: Might need adjustments. */
  2746. (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  2747. | SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
  2748. /* section flags */
  2749. 0, /* leading underscore */
  2750. ' ', /* ar_pad_char */
  2751. 16, /* ar_max_namelen */
  2752. 0, /* match priority. */
  2753. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  2754. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  2755. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  2756. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  2757. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  2758. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  2759. {
  2760. _bfd_dummy_target,
  2761. mmo_object_p, /* bfd_check_format */
  2762. _bfd_dummy_target,
  2763. _bfd_dummy_target,
  2764. },
  2765. {
  2766. bfd_false,
  2767. mmo_mkobject,
  2768. bfd_false,
  2769. bfd_false,
  2770. },
  2771. { /* bfd_write_contents */
  2772. bfd_false,
  2773. mmo_write_object_contents,
  2774. bfd_false,
  2775. bfd_false,
  2776. },
  2777. BFD_JUMP_TABLE_GENERIC (mmo),
  2778. BFD_JUMP_TABLE_COPY (mmo),
  2779. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  2780. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  2781. BFD_JUMP_TABLE_SYMBOLS (mmo),
  2782. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  2783. BFD_JUMP_TABLE_WRITE (mmo),
  2784. BFD_JUMP_TABLE_LINK (mmo),
  2785. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  2786. NULL,
  2787. NULL
  2788. };