mmo.c 100 KB

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