internals.texi 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. \input texinfo
  2. @c Copyright (C) 1991-2015 Free Software Foundation, Inc.
  3. @setfilename internals.info
  4. @node Top
  5. @top Assembler Internals
  6. @raisesections
  7. @cindex internals
  8. This chapter describes the internals of the assembler. It is incomplete, but
  9. it may help a bit.
  10. This chapter is not updated regularly, and it may be out of date.
  11. @menu
  12. * Data types:: Data types
  13. * GAS processing:: What GAS does when it runs
  14. * Porting GAS:: Porting GAS
  15. * Relaxation:: Relaxation
  16. * Broken words:: Broken words
  17. * Internal functions:: Internal functions
  18. * Test suite:: Test suite
  19. @end menu
  20. @node Data types
  21. @section Data types
  22. @cindex internals, data types
  23. This section describes some fundamental GAS data types.
  24. @menu
  25. * Symbols:: The symbolS structure
  26. * Expressions:: The expressionS structure
  27. * Fixups:: The fixS structure
  28. * Frags:: The fragS structure
  29. @end menu
  30. @node Symbols
  31. @subsection Symbols
  32. @cindex internals, symbols
  33. @cindex symbols, internal
  34. @cindex symbolS structure
  35. The definition for the symbol structure, @code{symbolS}, is located in
  36. @file{struc-symbol.h}.
  37. In general, the fields of this structure may not be referred to directly.
  38. Instead, you must use one of the accessor functions defined in @file{symbol.h}.
  39. These accessor functions should work for any GAS version.
  40. Symbol structures contain the following fields:
  41. @table @code
  42. @item sy_value
  43. This is an @code{expressionS} that describes the value of the symbol. It might
  44. refer to one or more other symbols; if so, its true value may not be known
  45. until @code{resolve_symbol_value} is called with @var{finalize_syms} non-zero
  46. in @code{write_object_file}.
  47. The expression is often simply a constant. Before @code{resolve_symbol_value}
  48. is called with @var{finalize_syms} set, the value is the offset from the frag
  49. (@pxref{Frags}). Afterward, the frag address has been added in.
  50. @item sy_resolved
  51. This field is non-zero if the symbol's value has been completely resolved. It
  52. is used during the final pass over the symbol table.
  53. @item sy_resolving
  54. This field is used to detect loops while resolving the symbol's value.
  55. @item sy_used_in_reloc
  56. This field is non-zero if the symbol is used by a relocation entry. If a local
  57. symbol is used in a relocation entry, it must be possible to redirect those
  58. relocations to other symbols, or this symbol cannot be removed from the final
  59. symbol list.
  60. @item sy_next
  61. @itemx sy_previous
  62. These pointers to other @code{symbolS} structures describe a doubly
  63. linked list. These fields should be accessed with
  64. the @code{symbol_next} and @code{symbol_previous} macros.
  65. @item sy_frag
  66. This points to the frag (@pxref{Frags}) that this symbol is attached to.
  67. @item sy_used
  68. Whether the symbol is used as an operand or in an expression. Note: Not all of
  69. the backends keep this information accurate; backends which use this bit are
  70. responsible for setting it when a symbol is used in backend routines.
  71. @item sy_mri_common
  72. Whether the symbol is an MRI common symbol created by the @code{COMMON}
  73. pseudo-op when assembling in MRI mode.
  74. @item sy_volatile
  75. Whether the symbol can be re-defined.
  76. @item sy_forward_ref
  77. Whether the symbol's value must only be evaluated upon use.
  78. @item sy_weakrefr
  79. Whether the symbol is a @code{weakref} alias to another symbol.
  80. @item sy_weakrefd
  81. Whether the symbol is or was referenced by one or more @code{weakref} aliases,
  82. and has not had any direct references.
  83. @item bsym
  84. This points to the BFD @code{asymbol} that
  85. will be used in writing the object file.
  86. @item sy_obj
  87. This format-specific data is of type @code{OBJ_SYMFIELD_TYPE}. If no macro by
  88. that name is defined in @file{obj-format.h}, this field is not defined.
  89. @item sy_tc
  90. This processor-specific data is of type @code{TC_SYMFIELD_TYPE}. If no macro
  91. by that name is defined in @file{targ-cpu.h}, this field is not defined.
  92. @end table
  93. Here is a description of the accessor functions. These should be used rather
  94. than referring to the fields of @code{symbolS} directly.
  95. @table @code
  96. @item S_SET_VALUE
  97. @cindex S_SET_VALUE
  98. Set the symbol's value.
  99. @item S_GET_VALUE
  100. @cindex S_GET_VALUE
  101. Get the symbol's value. This will cause @code{resolve_symbol_value} to be
  102. called if necessary.
  103. @item S_SET_SEGMENT
  104. @cindex S_SET_SEGMENT
  105. Set the section of the symbol.
  106. @item S_GET_SEGMENT
  107. @cindex S_GET_SEGMENT
  108. Get the symbol's section.
  109. @item S_GET_NAME
  110. @cindex S_GET_NAME
  111. Get the name of the symbol.
  112. @item S_SET_NAME
  113. @cindex S_SET_NAME
  114. Set the name of the symbol.
  115. @item S_IS_EXTERNAL
  116. @cindex S_IS_EXTERNAL
  117. Return non-zero if the symbol is externally visible.
  118. @item S_IS_EXTERN
  119. @cindex S_IS_EXTERN
  120. A synonym for @code{S_IS_EXTERNAL}. Don't use it.
  121. @item S_IS_WEAK
  122. @cindex S_IS_WEAK
  123. Return non-zero if the symbol is weak, or if it is a @code{weakref} alias or
  124. symbol that has not been strongly referenced.
  125. @item S_IS_WEAKREFR
  126. @cindex S_IS_WEAKREFR
  127. Return non-zero if the symbol is a @code{weakref} alias.
  128. @item S_IS_WEAKREFD
  129. @cindex S_IS_WEAKREFD
  130. Return non-zero if the symbol was aliased by a @code{weakref} alias and has not
  131. had any strong references.
  132. @item S_IS_VOLATILE
  133. @cindex S_IS_VOLATILE
  134. Return non-zero if the symbol may be re-defined. Such symbols get created by
  135. the @code{=} operator, @code{equ}, or @code{set}.
  136. @item S_IS_FORWARD_REF
  137. @cindex S_IS_FORWARD_REF
  138. Return non-zero if the symbol is a forward reference, that is its value must
  139. only be determined upon use.
  140. @item S_IS_COMMON
  141. @cindex S_IS_COMMON
  142. Return non-zero if this is a common symbol. Common symbols are sometimes
  143. represented as undefined symbols with a value, in which case this function will
  144. not be reliable.
  145. @item S_IS_DEFINED
  146. @cindex S_IS_DEFINED
  147. Return non-zero if this symbol is defined. This function is not reliable when
  148. called on a common symbol.
  149. @item S_IS_DEBUG
  150. @cindex S_IS_DEBUG
  151. Return non-zero if this is a debugging symbol.
  152. @item S_IS_LOCAL
  153. @cindex S_IS_LOCAL
  154. Return non-zero if this is a local assembler symbol which should not be
  155. included in the final symbol table. Note that this is not the opposite of
  156. @code{S_IS_EXTERNAL}. The @samp{-L} assembler option affects the return value
  157. of this function.
  158. @item S_SET_EXTERNAL
  159. @cindex S_SET_EXTERNAL
  160. Mark the symbol as externally visible.
  161. @item S_CLEAR_EXTERNAL
  162. @cindex S_CLEAR_EXTERNAL
  163. Mark the symbol as not externally visible.
  164. @item S_SET_WEAK
  165. @cindex S_SET_WEAK
  166. Mark the symbol as weak.
  167. @item S_SET_WEAKREFR
  168. @cindex S_SET_WEAKREFR
  169. Mark the symbol as the referrer in a @code{weakref} directive. The symbol it
  170. aliases must have been set to the value expression before this point. If the
  171. alias has already been used, the symbol is marked as used too.
  172. @item S_CLEAR_WEAKREFR
  173. @cindex S_CLEAR_WEAKREFR
  174. Clear the @code{weakref} alias status of a symbol. This is implicitly called
  175. whenever a symbol is defined or set to a new expression.
  176. @item S_SET_WEAKREFD
  177. @cindex S_SET_WEAKREFD
  178. Mark the symbol as the referred symbol in a @code{weakref} directive.
  179. Implicitly marks the symbol as weak, but see below. It should only be called
  180. if the referenced symbol has just been added to the symbol table.
  181. @item S_SET_WEAKREFD
  182. @cindex S_SET_WEAKREFD
  183. Clear the @code{weakref} aliased status of a symbol. This is implicitly called
  184. whenever the symbol is looked up, as part of a direct reference or a
  185. definition, but not as part of a @code{weakref} directive.
  186. @item S_SET_VOLATILE
  187. @cindex S_SET_VOLATILE
  188. Indicate that the symbol may be re-defined.
  189. @item S_CLEAR_VOLATILE
  190. @cindex S_CLEAR_VOLATILE
  191. Indicate that the symbol may no longer be re-defined.
  192. @item S_SET_FORWARD_REF
  193. @cindex S_SET_FORWARD_REF
  194. Indicate that the symbol is a forward reference, that is its value must only
  195. be determined upon use.
  196. @item S_GET_TYPE
  197. @itemx S_GET_DESC
  198. @itemx S_GET_OTHER
  199. @cindex S_GET_TYPE
  200. @cindex S_GET_DESC
  201. @cindex S_GET_OTHER
  202. Get the @code{type}, @code{desc}, and @code{other} fields of the symbol. These
  203. are only defined for object file formats for which they make sense (primarily
  204. a.out).
  205. @item S_SET_TYPE
  206. @itemx S_SET_DESC
  207. @itemx S_SET_OTHER
  208. @cindex S_SET_TYPE
  209. @cindex S_SET_DESC
  210. @cindex S_SET_OTHER
  211. Set the @code{type}, @code{desc}, and @code{other} fields of the symbol. These
  212. are only defined for object file formats for which they make sense (primarily
  213. a.out).
  214. @item S_GET_SIZE
  215. @cindex S_GET_SIZE
  216. Get the size of a symbol. This is only defined for object file formats for
  217. which it makes sense (primarily ELF).
  218. @item S_SET_SIZE
  219. @cindex S_SET_SIZE
  220. Set the size of a symbol. This is only defined for object file formats for
  221. which it makes sense (primarily ELF).
  222. @item symbol_get_value_expression
  223. @cindex symbol_get_value_expression
  224. Get a pointer to an @code{expressionS} structure which represents the value of
  225. the symbol as an expression.
  226. @item symbol_set_value_expression
  227. @cindex symbol_set_value_expression
  228. Set the value of a symbol to an expression.
  229. @item symbol_set_frag
  230. @cindex symbol_set_frag
  231. Set the frag where a symbol is defined.
  232. @item symbol_get_frag
  233. @cindex symbol_get_frag
  234. Get the frag where a symbol is defined.
  235. @item symbol_mark_used
  236. @cindex symbol_mark_used
  237. Mark a symbol as having been used in an expression.
  238. @item symbol_clear_used
  239. @cindex symbol_clear_used
  240. Clear the mark indicating that a symbol was used in an expression.
  241. @item symbol_used_p
  242. @cindex symbol_used_p
  243. Return whether a symbol was used in an expression.
  244. @item symbol_mark_used_in_reloc
  245. @cindex symbol_mark_used_in_reloc
  246. Mark a symbol as having been used by a relocation.
  247. @item symbol_clear_used_in_reloc
  248. @cindex symbol_clear_used_in_reloc
  249. Clear the mark indicating that a symbol was used in a relocation.
  250. @item symbol_used_in_reloc_p
  251. @cindex symbol_used_in_reloc_p
  252. Return whether a symbol was used in a relocation.
  253. @item symbol_mark_mri_common
  254. @cindex symbol_mark_mri_common
  255. Mark a symbol as an MRI common symbol.
  256. @item symbol_clear_mri_common
  257. @cindex symbol_clear_mri_common
  258. Clear the mark indicating that a symbol is an MRI common symbol.
  259. @item symbol_mri_common_p
  260. @cindex symbol_mri_common_p
  261. Return whether a symbol is an MRI common symbol.
  262. @item symbol_mark_written
  263. @cindex symbol_mark_written
  264. Mark a symbol as having been written.
  265. @item symbol_clear_written
  266. @cindex symbol_clear_written
  267. Clear the mark indicating that a symbol was written.
  268. @item symbol_written_p
  269. @cindex symbol_written_p
  270. Return whether a symbol was written.
  271. @item symbol_mark_resolved
  272. @cindex symbol_mark_resolved
  273. Mark a symbol as having been resolved.
  274. @item symbol_resolved_p
  275. @cindex symbol_resolved_p
  276. Return whether a symbol has been resolved.
  277. @item symbol_section_p
  278. @cindex symbol_section_p
  279. Return whether a symbol is a section symbol.
  280. @item symbol_equated_p
  281. @cindex symbol_equated_p
  282. Return whether a symbol is equated to another symbol.
  283. @item symbol_constant_p
  284. @cindex symbol_constant_p
  285. Return whether a symbol has a constant value, including being an offset within
  286. some frag.
  287. @item symbol_get_bfdsym
  288. @cindex symbol_get_bfdsym
  289. Return the BFD symbol associated with a symbol.
  290. @item symbol_set_bfdsym
  291. @cindex symbol_set_bfdsym
  292. Set the BFD symbol associated with a symbol.
  293. @item symbol_get_obj
  294. @cindex symbol_get_obj
  295. Return a pointer to the @code{OBJ_SYMFIELD_TYPE} field of a symbol.
  296. @item symbol_set_obj
  297. @cindex symbol_set_obj
  298. Set the @code{OBJ_SYMFIELD_TYPE} field of a symbol.
  299. @item symbol_get_tc
  300. @cindex symbol_get_tc
  301. Return a pointer to the @code{TC_SYMFIELD_TYPE} field of a symbol.
  302. @item symbol_set_tc
  303. @cindex symbol_set_tc
  304. Set the @code{TC_SYMFIELD_TYPE} field of a symbol.
  305. @end table
  306. GAS attempts to store local
  307. symbols--symbols which will not be written to the output file--using a
  308. different structure, @code{struct local_symbol}. This structure can only
  309. represent symbols whose value is an offset within a frag.
  310. Code outside of the symbol handler will always deal with @code{symbolS}
  311. structures and use the accessor functions. The accessor functions correctly
  312. deal with local symbols. @code{struct local_symbol} is much smaller than
  313. @code{symbolS} (which also automatically creates a bfd @code{asymbol}
  314. structure), so this saves space when assembling large files.
  315. The first field of @code{symbolS} is @code{bsym}, the pointer to the BFD
  316. symbol. The first field of @code{struct local_symbol} is a pointer which is
  317. always set to NULL. This is how the symbol accessor functions can distinguish
  318. local symbols from ordinary symbols. The symbol accessor functions
  319. automatically convert a local symbol into an ordinary symbol when necessary.
  320. @node Expressions
  321. @subsection Expressions
  322. @cindex internals, expressions
  323. @cindex expressions, internal
  324. @cindex expressionS structure
  325. Expressions are stored in an @code{expressionS} structure. The structure is
  326. defined in @file{expr.h}.
  327. @cindex expression
  328. The macro @code{expression} will create an @code{expressionS} structure based
  329. on the text found at the global variable @code{input_line_pointer}.
  330. @cindex make_expr_symbol
  331. @cindex expr_symbol_where
  332. A single @code{expressionS} structure can represent a single operation.
  333. Complex expressions are formed by creating @dfn{expression symbols} and
  334. combining them in @code{expressionS} structures. An expression symbol is
  335. created by calling @code{make_expr_symbol}. An expression symbol should
  336. naturally never appear in a symbol table, and the implementation of
  337. @code{S_IS_LOCAL} (@pxref{Symbols}) reflects that. The function
  338. @code{expr_symbol_where} returns non-zero if a symbol is an expression symbol,
  339. and also returns the file and line for the expression which caused it to be
  340. created.
  341. The @code{expressionS} structure has two symbol fields, a number field, an
  342. operator field, and a field indicating whether the number is unsigned.
  343. The operator field is of type @code{operatorT}, and describes how to interpret
  344. the other fields; see the definition in @file{expr.h} for the possibilities.
  345. An @code{operatorT} value of @code{O_big} indicates either a floating point
  346. number, stored in the global variable @code{generic_floating_point_number}, or
  347. an integer too large to store in an @code{offsetT} type, stored in the global
  348. array @code{generic_bignum}. This rather inflexible approach makes it
  349. impossible to use floating point numbers or large expressions in complex
  350. expressions.
  351. @node Fixups
  352. @subsection Fixups
  353. @cindex internals, fixups
  354. @cindex fixups
  355. @cindex fixS structure
  356. A @dfn{fixup} is basically anything which can not be resolved in the first
  357. pass. Sometimes a fixup can be resolved by the end of the assembly; if not,
  358. the fixup becomes a relocation entry in the object file.
  359. @cindex fix_new
  360. @cindex fix_new_exp
  361. A fixup is created by a call to @code{fix_new} or @code{fix_new_exp}. Both
  362. take a frag (@pxref{Frags}), a position within the frag, a size, an indication
  363. of whether the fixup is PC relative, and a type.
  364. The type is nominally a @code{bfd_reloc_code_real_type}, but several
  365. targets use other type codes to represent fixups that can not be described as
  366. relocations.
  367. The @code{fixS} structure has a number of fields, several of which are obsolete
  368. or are only used by a particular target. The important fields are:
  369. @table @code
  370. @item fx_frag
  371. The frag (@pxref{Frags}) this fixup is in.
  372. @item fx_where
  373. The location within the frag where the fixup occurs.
  374. @item fx_addsy
  375. The symbol this fixup is against. Typically, the value of this symbol is added
  376. into the object contents. This may be NULL.
  377. @item fx_subsy
  378. The value of this symbol is subtracted from the object contents. This is
  379. normally NULL.
  380. @item fx_offset
  381. A number which is added into the fixup.
  382. @item fx_addnumber
  383. Some CPU backends use this field to convey information between
  384. @code{md_apply_fix} and @code{tc_gen_reloc}. The machine independent code does
  385. not use it.
  386. @item fx_next
  387. The next fixup in the section.
  388. @item fx_r_type
  389. The type of the fixup.
  390. @item fx_size
  391. The size of the fixup. This is mostly used for error checking.
  392. @item fx_pcrel
  393. Whether the fixup is PC relative.
  394. @item fx_done
  395. Non-zero if the fixup has been applied, and no relocation entry needs to be
  396. generated.
  397. @item fx_file
  398. @itemx fx_line
  399. The file and line where the fixup was created.
  400. @item tc_fix_data
  401. This has the type @code{TC_FIX_TYPE}, and is only defined if the target defines
  402. that macro.
  403. @end table
  404. @node Frags
  405. @subsection Frags
  406. @cindex internals, frags
  407. @cindex frags
  408. @cindex fragS structure.
  409. The @code{fragS} structure is defined in @file{as.h}. Each frag represents a
  410. portion of the final object file. As GAS reads the source file, it creates
  411. frags to hold the data that it reads. At the end of the assembly the frags and
  412. fixups are processed to produce the final contents.
  413. @table @code
  414. @item fr_address
  415. The address of the frag. This is not set until the assembler rescans the list
  416. of all frags after the entire input file is parsed. The function
  417. @code{relax_segment} fills in this field.
  418. @item fr_next
  419. Pointer to the next frag in this (sub)section.
  420. @item fr_fix
  421. Fixed number of characters we know we're going to emit to the output file. May
  422. be zero.
  423. @item fr_var
  424. Variable number of characters we may output, after the initial @code{fr_fix}
  425. characters. May be zero.
  426. @item fr_offset
  427. The interpretation of this field is controlled by @code{fr_type}. Generally,
  428. if @code{fr_var} is non-zero, this is a repeat count: the @code{fr_var}
  429. characters are output @code{fr_offset} times.
  430. @item line
  431. Holds line number info when an assembler listing was requested.
  432. @item fr_type
  433. Relaxation state. This field indicates the interpretation of @code{fr_offset},
  434. @code{fr_symbol} and the variable-length tail of the frag, as well as the
  435. treatment it gets in various phases of processing. It does not affect the
  436. initial @code{fr_fix} characters; they are always supposed to be output
  437. verbatim (fixups aside). See below for specific values this field can have.
  438. @item fr_subtype
  439. Relaxation substate. If the macro @code{md_relax_frag} isn't defined, this is
  440. assumed to be an index into @code{TC_GENERIC_RELAX_TABLE} for the generic
  441. relaxation code to process (@pxref{Relaxation}). If @code{md_relax_frag} is
  442. defined, this field is available for any use by the CPU-specific code.
  443. @item fr_symbol
  444. This normally indicates the symbol to use when relaxing the frag according to
  445. @code{fr_type}.
  446. @item fr_opcode
  447. Points to the lowest-addressed byte of the opcode, for use in relaxation.
  448. @item tc_frag_data
  449. Target specific fragment data of type TC_FRAG_TYPE.
  450. Only present if @code{TC_FRAG_TYPE} is defined.
  451. @item fr_file
  452. @itemx fr_line
  453. The file and line where this frag was last modified.
  454. @item fr_literal
  455. Declared as a one-character array, this last field grows arbitrarily large to
  456. hold the actual contents of the frag.
  457. @end table
  458. These are the possible relaxation states, provided in the enumeration type
  459. @code{relax_stateT}, and the interpretations they represent for the other
  460. fields:
  461. @table @code
  462. @item rs_align
  463. @itemx rs_align_code
  464. The start of the following frag should be aligned on some boundary. In this
  465. frag, @code{fr_offset} is the logarithm (base 2) of the alignment in bytes.
  466. (For example, if alignment on an 8-byte boundary were desired, @code{fr_offset}
  467. would have a value of 3.) The variable characters indicate the fill pattern to
  468. be used. The @code{fr_subtype} field holds the maximum number of bytes to skip
  469. when doing this alignment. If more bytes are needed, the alignment is not
  470. done. An @code{fr_subtype} value of 0 means no maximum, which is the normal
  471. case. Target backends can use @code{rs_align_code} to handle certain types of
  472. alignment differently.
  473. @item rs_broken_word
  474. This indicates that ``broken word'' processing should be done (@pxref{Broken
  475. words}). If broken word processing is not necessary on the target machine,
  476. this enumerator value will not be defined.
  477. @item rs_cfa
  478. This state is used to implement exception frame optimizations. The
  479. @code{fr_symbol} is an expression symbol for the subtraction which may be
  480. relaxed. The @code{fr_opcode} field holds the frag for the preceding command
  481. byte. The @code{fr_offset} field holds the offset within that frag. The
  482. @code{fr_subtype} field is used during relaxation to hold the current size of
  483. the frag.
  484. @item rs_fill
  485. The variable characters are to be repeated @code{fr_offset} times. If
  486. @code{fr_offset} is 0, this frag has a length of @code{fr_fix}. Most frags
  487. have this type.
  488. @item rs_leb128
  489. This state is used to implement the DWARF ``little endian base 128''
  490. variable length number format. The @code{fr_symbol} is always an expression
  491. symbol, as constant expressions are emitted directly. The @code{fr_offset}
  492. field is used during relaxation to hold the previous size of the number so
  493. that we can determine if the fragment changed size.
  494. @item rs_machine_dependent
  495. Displacement relaxation is to be done on this frag. The target is indicated by
  496. @code{fr_symbol} and @code{fr_offset}, and @code{fr_subtype} indicates the
  497. particular machine-specific addressing mode desired. @xref{Relaxation}.
  498. @item rs_org
  499. The start of the following frag should be pushed back to some specific offset
  500. within the section. (Some assemblers use the value as an absolute address; GAS
  501. does not handle final absolute addresses, but rather requires that the linker
  502. set them.) The offset is given by @code{fr_symbol} and @code{fr_offset}; one
  503. character from the variable-length tail is used as the fill character.
  504. @end table
  505. @cindex frchainS structure
  506. A chain of frags is built up for each subsection. The data structure
  507. describing a chain is called a @code{frchainS}, and contains the following
  508. fields:
  509. @table @code
  510. @item frch_root
  511. Points to the first frag in the chain. May be NULL if there are no frags in
  512. this chain.
  513. @item frch_last
  514. Points to the last frag in the chain, or NULL if there are none.
  515. @item frch_next
  516. Next in the list of @code{frchainS} structures.
  517. @item frch_seg
  518. Indicates the section this frag chain belongs to.
  519. @item frch_subseg
  520. Subsection (subsegment) number of this frag chain.
  521. @item fix_root, fix_tail
  522. Point to first and last @code{fixS} structures associated with this subsection.
  523. @item frch_obstack
  524. Not currently used. Intended to be used for frag allocation for this
  525. subsection. This should reduce frag generation caused by switching sections.
  526. @item frch_frag_now
  527. The current frag for this subsegment.
  528. @end table
  529. A @code{frchainS} corresponds to a subsection; each section has a list of
  530. @code{frchainS} records associated with it. In most cases, only one subsection
  531. of each section is used, so the list will only be one element long, but any
  532. processing of frag chains should be prepared to deal with multiple chains per
  533. section.
  534. After the input files have been completely processed, and no more frags are to
  535. be generated, the frag chains are joined into one per section for further
  536. processing. After this point, it is safe to operate on one chain per section.
  537. The assembler always has a current frag, named @code{frag_now}. More space is
  538. allocated for the current frag using the @code{frag_more} function; this
  539. returns a pointer to the amount of requested space. The function
  540. @code{frag_room} says by how much the current frag can be extended.
  541. Relaxing is done using variant frags allocated by @code{frag_var}
  542. or @code{frag_variant} (@pxref{Relaxation}).
  543. @node GAS processing
  544. @section What GAS does when it runs
  545. @cindex internals, overview
  546. This is a quick look at what an assembler run looks like.
  547. @itemize @bullet
  548. @item
  549. The assembler initializes itself by calling various init routines.
  550. @item
  551. For each source file, the @code{read_a_source_file} function reads in the file
  552. and parses it. The global variable @code{input_line_pointer} points to the
  553. current text; it is guaranteed to be correct up to the end of the line, but not
  554. farther.
  555. @item
  556. For each line, the assembler passes labels to the @code{colon} function, and
  557. isolates the first word. If it looks like a pseudo-op, the word is looked up
  558. in the pseudo-op hash table @code{po_hash} and dispatched to a pseudo-op
  559. routine. Otherwise, the target dependent @code{md_assemble} routine is called
  560. to parse the instruction.
  561. @item
  562. When pseudo-ops or instructions output data, they add it to a frag, calling
  563. @code{frag_more} to get space to store it in.
  564. @item
  565. Pseudo-ops and instructions can also output fixups created by @code{fix_new} or
  566. @code{fix_new_exp}.
  567. @item
  568. For certain targets, instructions can create variant frags which are used to
  569. store relaxation information (@pxref{Relaxation}).
  570. @item
  571. When the input file is finished, the @code{write_object_file} routine is
  572. called. It assigns addresses to all the frags (@code{relax_segment}), resolves
  573. all the fixups (@code{fixup_segment}), resolves all the symbol values (using
  574. @code{resolve_symbol_value}), and finally writes out the file.
  575. @end itemize
  576. @node Porting GAS
  577. @section Porting GAS
  578. @cindex porting
  579. Each GAS target specifies two main things: the CPU file and the object format
  580. file. Two main switches in the @file{configure.ac} file handle this. The
  581. first switches on CPU type to set the shell variable @code{cpu_type}. The
  582. second switches on the entire target to set the shell variable @code{fmt}.
  583. The configure script uses the value of @code{cpu_type} to select two files in
  584. the @file{config} directory: @file{tc-@var{CPU}.c} and @file{tc-@var{CPU}.h}.
  585. The configuration process will create a file named @file{targ-cpu.h} in the
  586. build directory which includes @file{tc-@var{CPU}.h}.
  587. The configure script also uses the value of @code{fmt} to select two files:
  588. @file{obj-@var{fmt}.c} and @file{obj-@var{fmt}.h}. The configuration process
  589. will create a file named @file{obj-format.h} in the build directory which
  590. includes @file{obj-@var{fmt}.h}.
  591. You can also set the emulation in the configure script by setting the @code{em}
  592. variable. Normally the default value of @samp{generic} is fine. The
  593. configuration process will create a file named @file{targ-env.h} in the build
  594. directory which includes @file{te-@var{em}.h}.
  595. There is a special case for COFF. For historical reason, the GNU COFF
  596. assembler doesn't follow the documented behavior on certain debug symbols for
  597. the compatibility with other COFF assemblers. A port can define
  598. @code{STRICTCOFF} in the configure script to make the GNU COFF assembler
  599. to follow the documented behavior.
  600. Porting GAS to a new CPU requires writing the @file{tc-@var{CPU}} files.
  601. Porting GAS to a new object file format requires writing the
  602. @file{obj-@var{fmt}} files. There is sometimes some interaction between these
  603. two files, but it is normally minimal.
  604. The best approach is, of course, to copy existing files. The documentation
  605. below assumes that you are looking at existing files to see usage details.
  606. These interfaces have grown over time, and have never been carefully thought
  607. out or designed. Nothing about the interfaces described here is cast in stone.
  608. It is possible that they will change from one version of the assembler to the
  609. next. Also, new macros are added all the time as they are needed.
  610. @menu
  611. * CPU backend:: Writing a CPU backend
  612. * Object format backend:: Writing an object format backend
  613. * Emulations:: Writing emulation files
  614. @end menu
  615. @node CPU backend
  616. @subsection Writing a CPU backend
  617. @cindex CPU backend
  618. @cindex @file{tc-@var{CPU}}
  619. The CPU backend files are the heart of the assembler. They are the only parts
  620. of the assembler which actually know anything about the instruction set of the
  621. processor.
  622. You must define a reasonably small list of macros and functions in the CPU
  623. backend files. You may define a large number of additional macros in the CPU
  624. backend files, not all of which are documented here. You must, of course,
  625. define macros in the @file{.h} file, which is included by every assembler
  626. source file. You may define the functions as macros in the @file{.h} file, or
  627. as functions in the @file{.c} file.
  628. @table @code
  629. @item TC_@var{CPU}
  630. @cindex TC_@var{CPU}
  631. By convention, you should define this macro in the @file{.h} file. For
  632. example, @file{tc-m68k.h} defines @code{TC_M68K}. You might have to use this
  633. if it is necessary to add CPU specific code to the object format file.
  634. @item TARGET_FORMAT
  635. This macro is the BFD target name to use when creating the output file. This
  636. will normally depend upon the @code{OBJ_@var{FMT}} macro.
  637. @item TARGET_ARCH
  638. This macro is the BFD architecture to pass to @code{bfd_set_arch_mach}.
  639. @item TARGET_MACH
  640. This macro is the BFD machine number to pass to @code{bfd_set_arch_mach}. If
  641. it is not defined, GAS will use 0.
  642. @item TARGET_BYTES_BIG_ENDIAN
  643. You should define this macro to be non-zero if the target is big endian, and
  644. zero if the target is little endian.
  645. @item md_shortopts
  646. @itemx md_longopts
  647. @itemx md_longopts_size
  648. @itemx md_parse_option
  649. @itemx md_show_usage
  650. @itemx md_after_parse_args
  651. @cindex md_shortopts
  652. @cindex md_longopts
  653. @cindex md_longopts_size
  654. @cindex md_parse_option
  655. @cindex md_show_usage
  656. @cindex md_after_parse_args
  657. GAS uses these variables and functions during option processing.
  658. @code{md_shortopts} is a @code{const char *} which GAS adds to the machine
  659. independent string passed to @code{getopt}. @code{md_longopts} is a
  660. @code{struct option []} which GAS adds to the machine independent long options
  661. passed to @code{getopt}; you may use @code{OPTION_MD_BASE}, defined in
  662. @file{as.h}, as the start of a set of long option indices, if necessary.
  663. @code{md_longopts_size} is a @code{size_t} holding the size @code{md_longopts}.
  664. GAS will call @code{md_parse_option} whenever @code{getopt} returns an
  665. unrecognized code, presumably indicating a special code value which appears in
  666. @code{md_longopts}. This function should return non-zero if it handled the
  667. option and zero otherwise. There is no need to print a message about an option
  668. not being recognized. This will be handled by the generic code.
  669. GAS will call @code{md_show_usage} when a usage message is printed; it should
  670. print a description of the machine specific options. @code{md_after_pase_args},
  671. if defined, is called after all options are processed, to let the backend
  672. override settings done by the generic option parsing.
  673. @item md_begin
  674. @cindex md_begin
  675. GAS will call this function at the start of the assembly, after the command
  676. line arguments have been parsed and all the machine independent initializations
  677. have been completed.
  678. @item md_cleanup
  679. @cindex md_cleanup
  680. If you define this macro, GAS will call it at the end of each input file.
  681. @item md_assemble
  682. @cindex md_assemble
  683. GAS will call this function for each input line which does not contain a
  684. pseudo-op. The argument is a null terminated string. The function should
  685. assemble the string as an instruction with operands. Normally
  686. @code{md_assemble} will do this by calling @code{frag_more} and writing out
  687. some bytes (@pxref{Frags}). @code{md_assemble} will call @code{fix_new} to
  688. create fixups as needed (@pxref{Fixups}). Targets which need to do special
  689. purpose relaxation will call @code{frag_var}.
  690. @item md_pseudo_table
  691. @cindex md_pseudo_table
  692. This is a const array of type @code{pseudo_typeS}. It is a mapping from
  693. pseudo-op names to functions. You should use this table to implement
  694. pseudo-ops which are specific to the CPU.
  695. @item tc_conditional_pseudoop
  696. @cindex tc_conditional_pseudoop
  697. If this macro is defined, GAS will call it with a @code{pseudo_typeS} argument.
  698. It should return non-zero if the pseudo-op is a conditional which controls
  699. whether code is assembled, such as @samp{.if}. GAS knows about the normal
  700. conditional pseudo-ops, and you should normally not have to define this macro.
  701. @item comment_chars
  702. @cindex comment_chars
  703. This is a null terminated @code{const char} array of characters which start a
  704. comment.
  705. @item tc_comment_chars
  706. @cindex tc_comment_chars
  707. If this macro is defined, GAS will use it instead of @code{comment_chars}.
  708. This has the advantage that this macro does not have to refer to a constant
  709. array.
  710. @item tc_symbol_chars
  711. @cindex tc_symbol_chars
  712. If this macro is defined, it is a pointer to a null terminated list of
  713. characters which may appear in an operand. GAS already assumes that all
  714. alphanumeric characters, and @samp{$}, @samp{.}, and @samp{_} may appear in an
  715. operand (see @samp{symbol_chars} in @file{app.c}). This macro may be defined
  716. to treat additional characters as appearing in an operand. This affects the
  717. way in which GAS removes whitespace before passing the string to
  718. @samp{md_assemble}.
  719. @item line_comment_chars
  720. @cindex line_comment_chars
  721. This is a null terminated @code{const char} array of characters which start a
  722. comment when they appear at the start of a line.
  723. @item line_separator_chars
  724. @cindex line_separator_chars
  725. This is a null terminated @code{const char} array of characters which separate
  726. lines (null and newline are such characters by default, and need not be
  727. listed in this array). Note that line_separator_chars do not separate lines
  728. if found in a comment, such as after a character in line_comment_chars or
  729. comment_chars.
  730. @item tc_line_separator_chars
  731. @cindex tc_line_separator_chars
  732. If this macro is defined, GAS will use it instead of
  733. @code{line_separator_chars}. This has the advantage that this macro does not
  734. have to refer to a constant array.
  735. @item EXP_CHARS
  736. @cindex EXP_CHARS
  737. This is a null terminated @code{const char} array of characters which may be
  738. used as the exponent character in a floating point number. This is normally
  739. @code{"eE"}.
  740. @item FLT_CHARS
  741. @cindex FLT_CHARS
  742. This is a null terminated @code{const char} array of characters which may be
  743. used to indicate a floating point constant. A zero followed by one of these
  744. characters is assumed to be followed by a floating point number; thus they
  745. operate the way that @code{0x} is used to indicate a hexadecimal constant.
  746. Usually this includes @samp{r} and @samp{f}.
  747. @item LEX_AT
  748. @cindex LEX_AT
  749. You may define this macro to the lexical type of the @kbd{@@} character. The
  750. default is zero.
  751. Lexical types are a combination of @code{LEX_NAME} and @code{LEX_BEGIN_NAME},
  752. both defined in @file{read.h}. @code{LEX_NAME} indicates that the character
  753. may appear in a name. @code{LEX_BEGIN_NAME} indicates that the character may
  754. appear at the beginning of a name.
  755. @item LEX_BR
  756. @cindex LEX_BR
  757. You may define this macro to the lexical type of the brace characters @kbd{@{},
  758. @kbd{@}}, @kbd{[}, and @kbd{]}. The default value is zero.
  759. @item LEX_PCT
  760. @cindex LEX_PCT
  761. You may define this macro to the lexical type of the @kbd{%} character. The
  762. default value is zero.
  763. @item LEX_QM
  764. @cindex LEX_QM
  765. You may define this macro to the lexical type of the @kbd{?} character. The
  766. default value it zero.
  767. @item LEX_DOLLAR
  768. @cindex LEX_DOLLAR
  769. You may define this macro to the lexical type of the @kbd{$} character. The
  770. default value is @code{LEX_NAME | LEX_BEGIN_NAME}.
  771. @item NUMBERS_WITH_SUFFIX
  772. @cindex NUMBERS_WITH_SUFFIX
  773. When this macro is defined to be non-zero, the parser allows the radix of a
  774. constant to be indicated with a suffix. Valid suffixes are binary (B),
  775. octal (Q), and hexadecimal (H). Case is not significant.
  776. @item SINGLE_QUOTE_STRINGS
  777. @cindex SINGLE_QUOTE_STRINGS
  778. If you define this macro, GAS will treat single quotes as string delimiters.
  779. Normally only double quotes are accepted as string delimiters.
  780. @item NO_STRING_ESCAPES
  781. @cindex NO_STRING_ESCAPES
  782. If you define this macro, GAS will not permit escape sequences in a string.
  783. @item ONLY_STANDARD_ESCAPES
  784. @cindex ONLY_STANDARD_ESCAPES
  785. If you define this macro, GAS will warn about the use of nonstandard escape
  786. sequences in a string.
  787. @item md_start_line_hook
  788. @cindex md_start_line_hook
  789. If you define this macro, GAS will call it at the start of each line.
  790. @item LABELS_WITHOUT_COLONS
  791. @cindex LABELS_WITHOUT_COLONS
  792. If you define this macro, GAS will assume that any text at the start of a line
  793. is a label, even if it does not have a colon.
  794. @item TC_START_LABEL
  795. @itemx TC_START_LABEL_WITHOUT_COLON
  796. @cindex TC_START_LABEL
  797. You may define this macro to control what GAS considers to be a label. The
  798. default definition is to accept any name followed by a colon character.
  799. @item TC_START_LABEL_WITHOUT_COLON
  800. @cindex TC_START_LABEL_WITHOUT_COLON
  801. Same as TC_START_LABEL, but should be used instead of TC_START_LABEL when
  802. LABELS_WITHOUT_COLONS is defined.
  803. @item TC_FAKE_LABEL
  804. @cindex TC_FAKE_LABEL
  805. You may define this macro to control what GAS considers to be a fake
  806. label. The default fake label is FAKE_LABEL_NAME.
  807. @item NO_PSEUDO_DOT
  808. @cindex NO_PSEUDO_DOT
  809. If you define this macro, GAS will not require pseudo-ops to start with a
  810. @kbd{.} character.
  811. @item TC_EQUAL_IN_INSN
  812. @cindex TC_EQUAL_IN_INSN
  813. If you define this macro, it should return nonzero if the instruction is
  814. permitted to contain an @kbd{=} character. GAS will call it with two
  815. arguments, the character before the @kbd{=} character, and the value of
  816. the string preceding the equal sign. GAS uses this macro to decide if a
  817. @kbd{=} is an assignment or an instruction.
  818. @item TC_EOL_IN_INSN
  819. @cindex TC_EOL_IN_INSN
  820. If you define this macro, it should return nonzero if the current input line
  821. pointer should be treated as the end of a line.
  822. @item TC_CASE_SENSITIVE
  823. @cindex TC_CASE_SENSITIVE
  824. Define this macro if instruction mnemonics and pseudos are case sensitive.
  825. The default is to have it undefined giving case insensitive names.
  826. @item md_parse_name
  827. @cindex md_parse_name
  828. If this macro is defined, GAS will call it for any symbol found in an
  829. expression. You can define this to handle special symbols in a special way.
  830. If a symbol always has a certain value, you should normally enter it in the
  831. symbol table, perhaps using @code{reg_section}.
  832. @item md_undefined_symbol
  833. @cindex md_undefined_symbol
  834. GAS will call this function when a symbol table lookup fails, before it
  835. creates a new symbol. Typically this would be used to supply symbols whose
  836. name or value changes dynamically, possibly in a context sensitive way.
  837. Predefined symbols with fixed values, such as register names or condition
  838. codes, are typically entered directly into the symbol table when @code{md_begin}
  839. is called. One argument is passed, a @code{char *} for the symbol.
  840. @item md_operand
  841. @cindex md_operand
  842. GAS will call this function with one argument, an @code{expressionS}
  843. pointer, for any expression that can not be recognized. When the function
  844. is called, @code{input_line_pointer} will point to the start of the
  845. expression.
  846. @item md_register_arithmetic
  847. @cindex md_register_arithmetic
  848. If this macro is defined and evaluates to zero then GAS will not fold
  849. expressions that add or subtract a constant to/from a register to give
  850. another register. For example GAS's default behaviour is to fold the
  851. expression "r8 + 1" into "r9", which is probably not the result
  852. intended by the programmer. The default is to allow such folding,
  853. since this maintains backwards compatibility with earlier releases of
  854. GAS.
  855. @item tc_unrecognized_line
  856. @cindex tc_unrecognized_line
  857. If you define this macro, GAS will call it when it finds a line that it can not
  858. parse.
  859. @item md_do_align
  860. @cindex md_do_align
  861. You may define this macro to handle an alignment directive. GAS will call it
  862. when the directive is seen in the input file. For example, the i386 backend
  863. uses this to generate efficient nop instructions of varying lengths, depending
  864. upon the number of bytes that the alignment will skip.
  865. @item HANDLE_ALIGN
  866. @cindex HANDLE_ALIGN
  867. You may define this macro to do special handling for an alignment directive.
  868. GAS will call it at the end of the assembly.
  869. @item TC_IMPLICIT_LCOMM_ALIGNMENT (@var{size}, @var{p2var})
  870. @cindex TC_IMPLICIT_LCOMM_ALIGNMENT
  871. An @code{.lcomm} directive with no explicit alignment parameter will use this
  872. macro to set @var{p2var} to the alignment that a request for @var{size} bytes
  873. will have. The alignment is expressed as a power of two. If no alignment
  874. should take place, the macro definition should do nothing. Some targets define
  875. a @code{.bss} directive that is also affected by this macro. The default
  876. definition will set @var{p2var} to the truncated power of two of sizes up to
  877. eight bytes.
  878. @item md_flush_pending_output
  879. @cindex md_flush_pending_output
  880. If you define this macro, GAS will call it each time it skips any space because of a
  881. space filling or alignment or data allocation pseudo-op.
  882. @item TC_PARSE_CONS_EXPRESSION
  883. @cindex TC_PARSE_CONS_EXPRESSION
  884. You may define this macro to parse an expression used in a data allocation
  885. pseudo-op such as @code{.word}. You can use this to recognize relocation
  886. directives that may appear in such directives.
  887. @item BITFIELD_CONS_EXPRESSION
  888. @cindex BITFIELD_CONS_EXPRESSION
  889. If you define this macro, GAS will recognize bitfield instructions in data
  890. allocation pseudo-ops, as used on the i960.
  891. @item REPEAT_CONS_EXPRESSION
  892. @cindex REPEAT_CONS_EXPRESSION
  893. If you define this macro, GAS will recognize repeat counts in data allocation
  894. pseudo-ops, as used on the MIPS.
  895. @item md_cons_align
  896. @cindex md_cons_align
  897. You may define this macro to do any special alignment before a data allocation
  898. pseudo-op.
  899. @item TC_CONS_FIX_NEW
  900. @cindex TC_CONS_FIX_NEW
  901. You may define this macro to generate a fixup for a data allocation pseudo-op.
  902. @item TC_ADDRESS_BYTES
  903. @cindex TC_ADDRESS_BYTES
  904. Define this macro to specify the number of bytes used to store an address.
  905. Used to implement @code{dc.a}. The target must have a reloc for this size.
  906. @item TC_INIT_FIX_DATA (@var{fixp})
  907. @cindex TC_INIT_FIX_DATA
  908. A C statement to initialize the target specific fields of fixup @var{fixp}.
  909. These fields are defined with the @code{TC_FIX_TYPE} macro.
  910. @item TC_FIX_DATA_PRINT (@var{stream}, @var{fixp})
  911. @cindex TC_FIX_DATA_PRINT
  912. A C statement to output target specific debugging information for
  913. fixup @var{fixp} to @var{stream}. This macro is called by @code{print_fixup}.
  914. @item TC_FRAG_INIT (@var{fragp})
  915. @cindex TC_FRAG_INIT
  916. A C statement to initialize the target specific fields of frag @var{fragp}.
  917. These fields are defined with the @code{TC_FRAG_TYPE} macro.
  918. @item md_number_to_chars
  919. @cindex md_number_to_chars
  920. This should just call either @code{number_to_chars_bigendian} or
  921. @code{number_to_chars_littleendian}, whichever is appropriate. On targets like
  922. the MIPS which support options to change the endianness, which function to call
  923. is a runtime decision. On other targets, @code{md_number_to_chars} can be a
  924. simple macro.
  925. @item md_atof (@var{type},@var{litP},@var{sizeP})
  926. @cindex md_atof
  927. This function is called to convert an ASCII string into a floating point value
  928. in format used by the CPU. It takes three arguments. The first is @var{type}
  929. which is a byte describing the type of floating point number to be created. It
  930. is one of the characters defined in the @code{FLT_CHARS} macro. Possible
  931. values are @var{'f'} or @var{'s'} for single precision, @var{'d'} or @var{'r'}
  932. for double precision and @var{'x'} or @var{'p'} for extended precision. Either
  933. lower or upper case versions of these letters can be used. Note: some targets
  934. do not support all of these types, and some targets may also support other
  935. types not mentioned here.
  936. The second parameter is @var{litP} which is a pointer to a byte array where the
  937. converted value should be stored. The value is converted into LITTLENUMs and
  938. is stored in the target's endian-ness order. (@var{LITTLENUM} is defined in
  939. gas/bignum.h). Single precision values occupy 2 littlenums. Double precision
  940. values occupy 4 littlenums and extended precision values occupy either 5 or 6
  941. littlenums, depending upon the target.
  942. The third argument is @var{sizeP}, which is a pointer to a integer that should
  943. be filled in with the number of chars emitted into the byte array.
  944. The function should return NULL upon success or an error string upon failure.
  945. @item TC_LARGEST_EXPONENT_IS_NORMAL
  946. @cindex TC_LARGEST_EXPONENT_IS_NORMAL (@var{precision})
  947. This macro is used only by @file{atof-ieee.c}. It should evaluate to true
  948. if floats of the given precision use the largest exponent for normal numbers
  949. instead of NaNs and infinities. @var{precision} is @samp{F_PRECISION} for
  950. single precision, @samp{D_PRECISION} for double precision, or
  951. @samp{X_PRECISION} for extended double precision.
  952. The macro has a default definition which returns 0 for all cases.
  953. @item WORKING_DOT_WORD
  954. @itemx md_short_jump_size
  955. @itemx md_long_jump_size
  956. @itemx md_create_short_jump
  957. @itemx md_create_long_jump
  958. @itemx TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
  959. @cindex WORKING_DOT_WORD
  960. @cindex md_short_jump_size
  961. @cindex md_long_jump_size
  962. @cindex md_create_short_jump
  963. @cindex md_create_long_jump
  964. @cindex TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
  965. If @code{WORKING_DOT_WORD} is defined, GAS will not do broken word processing
  966. (@pxref{Broken words}). Otherwise, you should set @code{md_short_jump_size} to
  967. the size of a short jump (a jump that is just long enough to jump around a
  968. number of long jumps) and @code{md_long_jump_size} to the size of a long jump
  969. (a jump that can go anywhere in the function). You should define
  970. @code{md_create_short_jump} to create a short jump around a number of long
  971. jumps, and define @code{md_create_long_jump} to create a long jump.
  972. If defined, the macro TC_CHECK_ADJUSTED_BROKEN_DOT_WORD will be called for each
  973. adjusted word just before the word is output. The macro takes two arguments,
  974. an @code{addressT} with the adjusted word and a pointer to the current
  975. @code{struct broken_word}.
  976. @item md_estimate_size_before_relax
  977. @cindex md_estimate_size_before_relax
  978. This function returns an estimate of the size of a @code{rs_machine_dependent}
  979. frag before any relaxing is done. It may also create any necessary
  980. relocations.
  981. @item md_relax_frag
  982. @cindex md_relax_frag
  983. This macro may be defined to relax a frag. GAS will call this with the
  984. segment, the frag, and the change in size of all previous frags;
  985. @code{md_relax_frag} should return the change in size of the frag.
  986. @xref{Relaxation}.
  987. @item TC_GENERIC_RELAX_TABLE
  988. @cindex TC_GENERIC_RELAX_TABLE
  989. If you do not define @code{md_relax_frag}, you may define
  990. @code{TC_GENERIC_RELAX_TABLE} as a table of @code{relax_typeS} structures. The
  991. machine independent code knows how to use such a table to relax PC relative
  992. references. See @file{tc-m68k.c} for an example. @xref{Relaxation}.
  993. @item md_prepare_relax_scan
  994. @cindex md_prepare_relax_scan
  995. If defined, it is a C statement that is invoked prior to scanning
  996. the relax table.
  997. @item LINKER_RELAXING_SHRINKS_ONLY
  998. @cindex LINKER_RELAXING_SHRINKS_ONLY
  999. If you define this macro, and the global variable @samp{linkrelax} is set
  1000. (because of a command line option, or unconditionally in @code{md_begin}), a
  1001. @samp{.align} directive will cause extra space to be allocated. The linker can
  1002. then discard this space when relaxing the section.
  1003. @item TC_LINKRELAX_FIXUP (@var{segT})
  1004. @cindex TC_LINKRELAX_FIXUP
  1005. If defined, this macro allows control over whether fixups for a
  1006. given section will be processed when the @var{linkrelax} variable is
  1007. set. The macro is given the N_TYPE bits for the section in its
  1008. @var{segT} argument. If the macro evaluates to a non-zero value
  1009. then the fixups will be converted into relocs, otherwise they will
  1010. be passed to @var{md_apply_fix} as normal.
  1011. @item md_convert_frag
  1012. @cindex md_convert_frag
  1013. GAS will call this for each rs_machine_dependent fragment.
  1014. The instruction is completed using the data from the relaxation pass.
  1015. It may also create any necessary relocations.
  1016. @xref{Relaxation}.
  1017. @item TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
  1018. @cindex TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
  1019. Specifies the value to be assigned to @code{finalize_syms} before the function
  1020. @code{size_segs} is called. Since @code{size_segs} calls @code{cvt_frag_to_fill}
  1021. which can call @code{md_convert_frag}, this constant governs whether the symbols
  1022. accessed in @code{md_convert_frag} will be fully resolved. In particular it
  1023. governs whether local symbols will have been resolved, and had their frag
  1024. information removed. Depending upon the processing performed by
  1025. @code{md_convert_frag} the frag information may or may not be necessary, as may
  1026. the resolved values of the symbols. The default value is 1.
  1027. @item TC_VALIDATE_FIX (@var{fixP}, @var{seg}, @var{skip})
  1028. @cindex TC_VALIDATE_FIX
  1029. This macro is evaluated for each fixup (when @var{linkrelax} is not set).
  1030. It may be used to change the fixup in @code{struct fix *@var{fixP}} before
  1031. the generic code sees it, or to fully process the fixup. In the latter case,
  1032. a @code{goto @var{skip}} will bypass the generic code.
  1033. @item md_apply_fix (@var{fixP}, @var{valP}, @var{seg})
  1034. @cindex md_apply_fix
  1035. GAS will call this for each fixup that passes the @code{TC_VALIDATE_FIX} test
  1036. when @var{linkrelax} is not set. It should store the correct value in the
  1037. object file. @code{struct fix *@var{fixP}} is the fixup @code{md_apply_fix}
  1038. is operating on. @code{valueT *@var{valP}} is the value to store into the
  1039. object files, or at least is the generic code's best guess. Specifically,
  1040. *@var{valP} is the value of the fixup symbol, perhaps modified by
  1041. @code{MD_APPLY_SYM_VALUE}, plus @code{@var{fixP}->fx_offset} (symbol addend),
  1042. less @code{MD_PCREL_FROM_SECTION} for pc-relative fixups.
  1043. @code{segT @var{seg}} is the section the fix is in.
  1044. @code{fixup_segment} performs a generic overflow check on *@var{valP} after
  1045. @code{md_apply_fix} returns. If the overflow check is relevant for the target
  1046. machine, then @code{md_apply_fix} should modify *@var{valP}, typically to the
  1047. value stored in the object file.
  1048. @item TC_FORCE_RELOCATION (@var{fix})
  1049. @cindex TC_FORCE_RELOCATION
  1050. If this macro returns non-zero, it guarantees that a relocation will be emitted
  1051. even when the value can be resolved locally, as @code{fixup_segment} tries to
  1052. reduce the number of relocations emitted. For example, a fixup expression
  1053. against an absolute symbol will normally not require a reloc. If undefined,
  1054. a default of @w{@code{(S_FORCE_RELOC ((@var{fix})->fx_addsy))}} is used.
  1055. @item TC_FORCE_RELOCATION_ABS (@var{fix})
  1056. @cindex TC_FORCE_RELOCATION_ABS
  1057. Like @code{TC_FORCE_RELOCATION}, but used only for fixup expressions against an
  1058. absolute symbol. If undefined, @code{TC_FORCE_RELOCATION} will be used.
  1059. @item TC_FORCE_RELOCATION_LOCAL (@var{fix})
  1060. @cindex TC_FORCE_RELOCATION_LOCAL
  1061. Like @code{TC_FORCE_RELOCATION}, but used only for fixup expressions against a
  1062. symbol in the current section. If undefined, fixups that are not
  1063. @code{fx_pcrel} or for which @code{TC_FORCE_RELOCATION}
  1064. returns non-zero, will emit relocs.
  1065. @item TC_FORCE_RELOCATION_SUB_SAME (@var{fix}, @var{seg})
  1066. @cindex TC_FORCE_RELOCATION_SUB_SAME
  1067. This macro controls resolution of fixup expressions involving the
  1068. difference of two symbols in the same section. If this macro returns zero,
  1069. the subtrahend will be resolved and @code{fx_subsy} set to @code{NULL} for
  1070. @code{md_apply_fix}. If undefined, the default of
  1071. @w{@code{! SEG_NORMAL (@var{seg})}} will be used.
  1072. @item TC_FORCE_RELOCATION_SUB_ABS (@var{fix}, @var{seg})
  1073. @cindex TC_FORCE_RELOCATION_SUB_ABS
  1074. Like @code{TC_FORCE_RELOCATION_SUB_SAME}, but used when the subtrahend is an
  1075. absolute symbol. If the macro is undefined a default of @code{0} is used.
  1076. @item TC_FORCE_RELOCATION_SUB_LOCAL (@var{fix}, @var{seg})
  1077. @cindex TC_FORCE_RELOCATION_SUB_LOCAL
  1078. Like @code{TC_FORCE_RELOCATION_SUB_ABS}, but the subtrahend is a symbol in the
  1079. same section as the fixup.
  1080. @item TC_VALIDATE_FIX_SUB (@var{fix}, @var{seg})
  1081. @cindex TC_VALIDATE_FIX_SUB
  1082. This macro is evaluated for any fixup with a @code{fx_subsy} that
  1083. @code{fixup_segment} cannot reduce to a number. If the macro returns
  1084. @code{false} an error will be reported.
  1085. @item TC_GLOBAL_REGISTER_SYMBOL_OK
  1086. @cindex TC_GLOBAL_REGISTER_SYMBOL_OK
  1087. Define this macro if global register symbols are supported. The default
  1088. is to disallow global register symbols.
  1089. @item MD_APPLY_SYM_VALUE (@var{fix})
  1090. @cindex MD_APPLY_SYM_VALUE
  1091. This macro controls whether the symbol value becomes part of the value passed
  1092. to @code{md_apply_fix}. If the macro is undefined, or returns non-zero, the
  1093. symbol value will be included. For ELF, a suitable definition might simply be
  1094. @code{0}, because ELF relocations don't include the symbol value in the addend.
  1095. @item S_FORCE_RELOC (@var{sym}, @var{strict})
  1096. @cindex S_FORCE_RELOC
  1097. This function returns true for symbols
  1098. that should not be reduced to section symbols or eliminated from expressions,
  1099. because they may be overridden by the linker. ie. for symbols that are
  1100. undefined or common, and when @var{strict} is set, weak, or global (for ELF
  1101. assemblers that support ELF shared library linking semantics).
  1102. @item EXTERN_FORCE_RELOC
  1103. @cindex EXTERN_FORCE_RELOC
  1104. This macro controls whether @code{S_FORCE_RELOC} returns true for global
  1105. symbols. If undefined, the default is @code{true} for ELF assemblers, and
  1106. @code{false} for non-ELF.
  1107. @item tc_gen_reloc
  1108. @cindex tc_gen_reloc
  1109. GAS will call this to generate a reloc. GAS will pass
  1110. the resulting reloc to @code{bfd_install_relocation}. This currently works
  1111. poorly, as @code{bfd_install_relocation} often does the wrong thing, and
  1112. instances of @code{tc_gen_reloc} have been written to work around the problems,
  1113. which in turns makes it difficult to fix @code{bfd_install_relocation}.
  1114. @item RELOC_EXPANSION_POSSIBLE
  1115. @cindex RELOC_EXPANSION_POSSIBLE
  1116. If you define this macro, it means that @code{tc_gen_reloc} may return multiple
  1117. relocation entries for a single fixup. In this case, the return value of
  1118. @code{tc_gen_reloc} is a pointer to a null terminated array.
  1119. @item MAX_RELOC_EXPANSION
  1120. @cindex MAX_RELOC_EXPANSION
  1121. You must define this if @code{RELOC_EXPANSION_POSSIBLE} is defined; it
  1122. indicates the largest number of relocs which @code{tc_gen_reloc} may return for
  1123. a single fixup.
  1124. @item tc_fix_adjustable
  1125. @cindex tc_fix_adjustable
  1126. You may define this macro to indicate whether a fixup against a locally defined
  1127. symbol should be adjusted to be against the section symbol. It should return a
  1128. non-zero value if the adjustment is acceptable.
  1129. @item MD_PCREL_FROM_SECTION (@var{fixp}, @var{section})
  1130. @cindex MD_PCREL_FROM_SECTION
  1131. If you define this macro, it should return the position from which the PC
  1132. relative adjustment for a PC relative fixup should be made. On many
  1133. processors, the base of a PC relative instruction is the next instruction,
  1134. so this macro would return the length of an instruction, plus the address of
  1135. the PC relative fixup. The latter can be calculated as
  1136. @var{fixp}->fx_where + @var{fixp}->fx_frag->fr_address .
  1137. @item md_pcrel_from
  1138. @cindex md_pcrel_from
  1139. This is the default value of @code{MD_PCREL_FROM_SECTION}. The difference is
  1140. that @code{md_pcrel_from} does not take a section argument.
  1141. @item tc_frob_label
  1142. @cindex tc_frob_label
  1143. If you define this macro, GAS will call it each time a label is defined.
  1144. @item tc_new_dot_label
  1145. @cindex tc_new_dot_label
  1146. If you define this macro, GAS will call it each time a fake label is created
  1147. off the special dot symbol.
  1148. @item md_section_align
  1149. @cindex md_section_align
  1150. GAS will call this function for each section at the end of the assembly, to
  1151. permit the CPU backend to adjust the alignment of a section. The function
  1152. must take two arguments, a @code{segT} for the section and a @code{valueT}
  1153. for the size of the section, and return a @code{valueT} for the rounded
  1154. size.
  1155. @item md_macro_start
  1156. @cindex md_macro_start
  1157. If defined, GAS will call this macro when it starts to include a macro
  1158. expansion. @code{macro_nest} indicates the current macro nesting level, which
  1159. includes the one being expanded.
  1160. @item md_macro_info
  1161. @cindex md_macro_info
  1162. If defined, GAS will call this macro after the macro expansion has been
  1163. included in the input and after parsing the macro arguments. The single
  1164. argument is a pointer to the macro processing's internal representation of the
  1165. macro (macro_entry *), which includes expansion of the formal arguments.
  1166. @item md_macro_end
  1167. @cindex md_macro_end
  1168. Complement to md_macro_start. If defined, it is called when finished
  1169. processing an inserted macro expansion, just before decrementing macro_nest.
  1170. @item DOUBLEBAR_PARALLEL
  1171. @cindex DOUBLEBAR_PARALLEL
  1172. Affects the preprocessor so that lines containing '||' don't have their
  1173. whitespace stripped following the double bar. This is useful for targets that
  1174. implement parallel instructions.
  1175. @item KEEP_WHITE_AROUND_COLON
  1176. @cindex KEEP_WHITE_AROUND_COLON
  1177. Normally, whitespace is compressed and removed when, in the presence of the
  1178. colon, the adjoining tokens can be distinguished. This option affects the
  1179. preprocessor so that whitespace around colons is preserved. This is useful
  1180. when colons might be removed from the input after preprocessing but before
  1181. assembling, so that adjoining tokens can still be distinguished if there is
  1182. whitespace, or concatenated if there is not.
  1183. @item tc_frob_section
  1184. @cindex tc_frob_section
  1185. If you define this macro, GAS will call it for each
  1186. section at the end of the assembly.
  1187. @item tc_frob_file_before_adjust
  1188. @cindex tc_frob_file_before_adjust
  1189. If you define this macro, GAS will call it after the symbol values are
  1190. resolved, but before the fixups have been changed from local symbols to section
  1191. symbols.
  1192. @item tc_frob_symbol
  1193. @cindex tc_frob_symbol
  1194. If you define this macro, GAS will call it for each symbol. You can indicate
  1195. that the symbol should not be included in the object file by defining this
  1196. macro to set its second argument to a non-zero value.
  1197. @item tc_frob_file
  1198. @cindex tc_frob_file
  1199. If you define this macro, GAS will call it after the symbol table has been
  1200. completed, but before the relocations have been generated.
  1201. @item tc_frob_file_after_relocs
  1202. If you define this macro, GAS will call it after the relocs have been
  1203. generated.
  1204. @item tc_cfi_reloc_for_encoding
  1205. @cindex tc_cfi_reloc_for_encoding
  1206. This macro is used to indicate whether a cfi encoding requires a relocation.
  1207. It should return the required relocation type. Defining this macro implies
  1208. that Compact EH is supported.
  1209. @item md_post_relax_hook
  1210. If you define this macro, GAS will call it after relaxing and sizing the
  1211. segments.
  1212. @item LISTING_HEADER
  1213. A string to use on the header line of a listing. The default value is simply
  1214. @code{"GAS LISTING"}.
  1215. @item LISTING_WORD_SIZE
  1216. The number of bytes to put into a word in a listing. This affects the way the
  1217. bytes are clumped together in the listing. For example, a value of 2 might
  1218. print @samp{1234 5678} where a value of 1 would print @samp{12 34 56 78}. The
  1219. default value is 4.
  1220. @item LISTING_LHS_WIDTH
  1221. The number of words of data to print on the first line of a listing for a
  1222. particular source line, where each word is @code{LISTING_WORD_SIZE} bytes. The
  1223. default value is 1.
  1224. @item LISTING_LHS_WIDTH_SECOND
  1225. Like @code{LISTING_LHS_WIDTH}, but applying to the second and subsequent line
  1226. of the data printed for a particular source line. The default value is 1.
  1227. @item LISTING_LHS_CONT_LINES
  1228. The maximum number of continuation lines to print in a listing for a particular
  1229. source line. The default value is 4.
  1230. @item LISTING_RHS_WIDTH
  1231. The maximum number of characters to print from one line of the input file. The
  1232. default value is 100.
  1233. @item TC_COFF_SECTION_DEFAULT_ATTRIBUTES
  1234. @cindex TC_COFF_SECTION_DEFAULT_ATTRIBUTES
  1235. The COFF @code{.section} directive will use the value of this macro to set
  1236. a new section's attributes when a directive has no valid flags or when the
  1237. flag is @code{w}. The default value of the macro is @code{SEC_LOAD | SEC_DATA}.
  1238. @item DWARF2_FORMAT (@var{sec})
  1239. @cindex DWARF2_FORMAT
  1240. If you define this, it should return one of @code{dwarf2_format_32bit},
  1241. @code{dwarf2_format_64bit}, or @code{dwarf2_format_64bit_irix} to indicate
  1242. the size of internal DWARF section offsets and the format of the DWARF initial
  1243. length fields. When @code{dwarf2_format_32bit} is returned, the initial
  1244. length field will be 4 bytes long and section offsets are 32 bits in size.
  1245. For @code{dwarf2_format_64bit} and @code{dwarf2_format_64bit_irix}, section
  1246. offsets are 64 bits in size, but the initial length field differs. An 8 byte
  1247. initial length is indicated by @code{dwarf2_format_64bit_irix} and
  1248. @code{dwarf2_format_64bit} indicates a 12 byte initial length field in
  1249. which the first four bytes are 0xffffffff and the next 8 bytes are
  1250. the section's length.
  1251. If you don't define this, @code{dwarf2_format_32bit} will be used as
  1252. the default.
  1253. This define only affects debug
  1254. sections generated by the assembler. DWARF 2 sections generated by
  1255. other tools will be unaffected by this setting.
  1256. @item DWARF2_ADDR_SIZE (@var{bfd})
  1257. @cindex DWARF2_ADDR_SIZE
  1258. It should return the size of an address, as it should be represented in
  1259. debugging info. If you don't define this macro, the default definition uses
  1260. the number of bits per address, as defined in @var{bfd}, divided by 8.
  1261. @item MD_DEBUG_FORMAT_SELECTOR
  1262. @cindex MD_DEBUG_FORMAT_SELECTOR
  1263. If defined this macro is the name of a function to be called when the
  1264. @samp{--gen-debug} switch is detected on the assembler's command line. The
  1265. prototype for the function looks like this:
  1266. @smallexample
  1267. enum debug_info_type MD_DEBUG_FORMAT_SELECTOR (int * use_gnu_extensions)
  1268. @end smallexample
  1269. The function should return the debug format that is preferred by the CPU
  1270. backend. This format will be used when generating assembler specific debug
  1271. information.
  1272. @item md_allow_local_subtract (@var{left}, @var{right}, @var{section})
  1273. If defined, GAS will call this macro when evaluating an expression which is the
  1274. difference of two symbols defined in the same section. It takes three
  1275. arguments: @code{expressioS * @var{left}} which is the symbolic expression on
  1276. the left hand side of the subtraction operation, @code{expressionS *
  1277. @var{right}} which is the symbolic expression on the right hand side of the
  1278. subtraction, and @code{segT @var{section}} which is the section containing the two
  1279. symbols. The macro should return a non-zero value if the expression should be
  1280. evaluated. Targets which implement link time relaxation which may change the
  1281. position of the two symbols relative to each other should ensure that this
  1282. macro returns zero in situations where this can occur.
  1283. @item md_allow_eh_opt
  1284. If defined, GAS will check this macro before performing any optimizations on
  1285. the DWARF call frame debug information that is emitted. Targets which
  1286. implement link time relaxation may need to define this macro and set it to zero
  1287. if it is possible to change the size of a function's prologue.
  1288. @end table
  1289. @node Object format backend
  1290. @subsection Writing an object format backend
  1291. @cindex object format backend
  1292. @cindex @file{obj-@var{fmt}}
  1293. As with the CPU backend, the object format backend must define a few things,
  1294. and may define some other things. The interface to the object format backend
  1295. is generally simpler; most of the support for an object file format consists of
  1296. defining a number of pseudo-ops.
  1297. The object format @file{.h} file must include @file{targ-cpu.h}.
  1298. @table @code
  1299. @item OBJ_@var{format}
  1300. @cindex OBJ_@var{format}
  1301. By convention, you should define this macro in the @file{.h} file. For
  1302. example, @file{obj-elf.h} defines @code{OBJ_ELF}. You might have to use this
  1303. if it is necessary to add object file format specific code to the CPU file.
  1304. @item obj_begin
  1305. If you define this macro, GAS will call it at the start of the assembly, after
  1306. the command line arguments have been parsed and all the machine independent
  1307. initializations have been completed.
  1308. @item obj_app_file
  1309. @cindex obj_app_file
  1310. If you define this macro, GAS will invoke it when it sees a @code{.file}
  1311. pseudo-op or a @samp{#} line as used by the C preprocessor.
  1312. @item OBJ_COPY_SYMBOL_ATTRIBUTES
  1313. @cindex OBJ_COPY_SYMBOL_ATTRIBUTES
  1314. You should define this macro to copy object format specific information from
  1315. one symbol to another. GAS will call it when one symbol is equated to
  1316. another.
  1317. @item obj_sec_sym_ok_for_reloc
  1318. @cindex obj_sec_sym_ok_for_reloc
  1319. You may define this macro to indicate that it is OK to use a section symbol in
  1320. a relocation entry. If it is not, GAS will define a new symbol at the start
  1321. of a section.
  1322. @item EMIT_SECTION_SYMBOLS
  1323. @cindex EMIT_SECTION_SYMBOLS
  1324. You should define this macro with a zero value if you do not want to include
  1325. section symbols in the output symbol table. The default value for this macro
  1326. is one.
  1327. @item obj_adjust_symtab
  1328. @cindex obj_adjust_symtab
  1329. If you define this macro, GAS will invoke it just before setting the symbol
  1330. table of the output BFD. For example, the COFF support uses this macro to
  1331. generate a @code{.file} symbol if none was generated previously.
  1332. @item SEPARATE_STAB_SECTIONS
  1333. @cindex SEPARATE_STAB_SECTIONS
  1334. You may define this macro to a nonzero value to indicate that stabs should be
  1335. placed in separate sections, as in ELF.
  1336. @item INIT_STAB_SECTION
  1337. @cindex INIT_STAB_SECTION
  1338. You may define this macro to initialize the stabs section in the output file.
  1339. @item OBJ_PROCESS_STAB
  1340. @cindex OBJ_PROCESS_STAB
  1341. You may define this macro to do specific processing on a stabs entry.
  1342. @item obj_frob_section
  1343. @cindex obj_frob_section
  1344. If you define this macro, GAS will call it for each section at the end of the
  1345. assembly.
  1346. @item obj_frob_file_before_adjust
  1347. @cindex obj_frob_file_before_adjust
  1348. If you define this macro, GAS will call it after the symbol values are
  1349. resolved, but before the fixups have been changed from local symbols to section
  1350. symbols.
  1351. @item obj_frob_symbol
  1352. @cindex obj_frob_symbol
  1353. If you define this macro, GAS will call it for each symbol. You can indicate
  1354. that the symbol should not be included in the object file by defining this
  1355. macro to set its second argument to a non-zero value.
  1356. @item obj_set_weak_hook
  1357. @cindex obj_set_weak_hook
  1358. If you define this macro, @code{S_SET_WEAK} will call it before modifying the
  1359. symbol's flags.
  1360. @item obj_clear_weak_hook
  1361. @cindex obj_clear_weak_hook
  1362. If you define this macro, @code{S_CLEAR_WEAKREFD} will call it after cleaning
  1363. the @code{weakrefd} flag, but before modifying any other flags.
  1364. @item obj_frob_file
  1365. @cindex obj_frob_file
  1366. If you define this macro, GAS will call it after the symbol table has been
  1367. completed, but before the relocations have been generated.
  1368. @item obj_frob_file_after_relocs
  1369. If you define this macro, GAS will call it after the relocs have been
  1370. generated.
  1371. @item SET_SECTION_RELOCS (@var{sec}, @var{relocs}, @var{n})
  1372. @cindex SET_SECTION_RELOCS
  1373. If you define this, it will be called after the relocations have been set for
  1374. the section @var{sec}. The list of relocations is in @var{relocs}, and the
  1375. number of relocations is in @var{n}.
  1376. @end table
  1377. @node Emulations
  1378. @subsection Writing emulation files
  1379. Normally you do not have to write an emulation file. You can just use
  1380. @file{te-generic.h}.
  1381. If you do write your own emulation file, it must include @file{obj-format.h}.
  1382. An emulation file will often define @code{TE_@var{EM}}; this may then be used
  1383. in other files to change the output.
  1384. @node Relaxation
  1385. @section Relaxation
  1386. @cindex relaxation
  1387. @dfn{Relaxation} is a generic term used when the size of some instruction or
  1388. data depends upon the value of some symbol or other data.
  1389. GAS knows to relax a particular type of PC relative relocation using a table.
  1390. You can also define arbitrarily complex forms of relaxation yourself.
  1391. @menu
  1392. * Relaxing with a table:: Relaxing with a table
  1393. * General relaxing:: General relaxing
  1394. @end menu
  1395. @node Relaxing with a table
  1396. @subsection Relaxing with a table
  1397. If you do not define @code{md_relax_frag}, and you do define
  1398. @code{TC_GENERIC_RELAX_TABLE}, GAS will relax @code{rs_machine_dependent} frags
  1399. based on the frag subtype and the displacement to some specified target
  1400. address. The basic idea is that several machines have different addressing
  1401. modes for instructions that can specify different ranges of values, with
  1402. successive modes able to access wider ranges, including the entirety of the
  1403. previous range. Smaller ranges are assumed to be more desirable (perhaps the
  1404. instruction requires one word instead of two or three); if this is not the
  1405. case, don't describe the smaller-range, inferior mode.
  1406. The @code{fr_subtype} field of a frag is an index into a CPU-specific
  1407. relaxation table. That table entry indicates the range of values that can be
  1408. stored, the number of bytes that will have to be added to the frag to
  1409. accommodate the addressing mode, and the index of the next entry to examine if
  1410. the value to be stored is outside the range accessible by the current
  1411. addressing mode. The @code{fr_symbol} field of the frag indicates what symbol
  1412. is to be accessed; the @code{fr_offset} field is added in.
  1413. If the @code{TC_PCREL_ADJUST} macro is defined, which currently should only happen
  1414. for the NS32k family, the @code{TC_PCREL_ADJUST} macro is called on the frag to
  1415. compute an adjustment to be made to the displacement.
  1416. The value fitted by the relaxation code is always assumed to be a displacement
  1417. from the current frag. (More specifically, from @code{fr_fix} bytes into the
  1418. frag.)
  1419. @ignore
  1420. This seems kinda silly. What about fitting small absolute values? I suppose
  1421. @code{md_assemble} is supposed to take care of that, but if the operand is a
  1422. difference between symbols, it might not be able to, if the difference was not
  1423. computable yet.
  1424. @end ignore
  1425. The end of the relaxation sequence is indicated by a ``next'' value of 0. This
  1426. means that the first entry in the table can't be used.
  1427. For some configurations, the linker can do relaxing within a section of an
  1428. object file. If call instructions of various sizes exist, the linker can
  1429. determine which should be used in each instance, when a symbol's value is
  1430. resolved. In order for the linker to avoid wasting space and having to insert
  1431. no-op instructions, it must be able to expand or shrink the section contents
  1432. while still preserving intra-section references and meeting alignment
  1433. requirements.
  1434. For the i960 using b.out format, no expansion is done; instead, each
  1435. @samp{.align} directive causes extra space to be allocated, enough that when
  1436. the linker is relaxing a section and removing unneeded space, it can discard
  1437. some or all of this extra padding and cause the following data to be correctly
  1438. aligned.
  1439. For the H8/300, I think the linker expands calls that can't reach, and doesn't
  1440. worry about alignment issues; the cpu probably never needs any significant
  1441. alignment beyond the instruction size.
  1442. The relaxation table type contains these fields:
  1443. @table @code
  1444. @item long rlx_forward
  1445. Forward reach, must be non-negative.
  1446. @item long rlx_backward
  1447. Backward reach, must be zero or negative.
  1448. @item rlx_length
  1449. Length in bytes of this addressing mode.
  1450. @item rlx_more
  1451. Index of the next-longer relax state, or zero if there is no next relax state.
  1452. @end table
  1453. The relaxation is done in @code{relax_segment} in @file{write.c}. The
  1454. difference in the length fields between the original mode and the one finally
  1455. chosen by the relaxing code is taken as the size by which the current frag will
  1456. be increased in size. For example, if the initial relaxing mode has a length
  1457. of 2 bytes, and because of the size of the displacement, it gets upgraded to a
  1458. mode with a size of 6 bytes, it is assumed that the frag will grow by 4 bytes.
  1459. (The initial two bytes should have been part of the fixed portion of the frag,
  1460. since it is already known that they will be output.) This growth must be
  1461. effected by @code{md_convert_frag}; it should increase the @code{fr_fix} field
  1462. by the appropriate size, and fill in the appropriate bytes of the frag.
  1463. (Enough space for the maximum growth should have been allocated in the call to
  1464. frag_var as the second argument.)
  1465. If relocation records are needed, they should be emitted by
  1466. @code{md_estimate_size_before_relax}. This function should examine the target
  1467. symbol of the supplied frag and correct the @code{fr_subtype} of the frag if
  1468. needed. When this function is called, if the symbol has not yet been defined,
  1469. it will not become defined later; however, its value may still change if the
  1470. section it is in gets relaxed.
  1471. Usually, if the symbol is in the same section as the frag (given by the
  1472. @var{sec} argument), the narrowest likely relaxation mode is stored in
  1473. @code{fr_subtype}, and that's that.
  1474. If the symbol is undefined, or in a different section (and therefore movable
  1475. to an arbitrarily large distance), the largest available relaxation mode is
  1476. specified, @code{fix_new} is called to produce the relocation record,
  1477. @code{fr_fix} is increased to include the relocated field (remember, this
  1478. storage was allocated when @code{frag_var} was called), and @code{frag_wane} is
  1479. called to convert the frag to an @code{rs_fill} frag with no variant part.
  1480. Sometimes changing addressing modes may also require rewriting the instruction.
  1481. It can be accessed via @code{fr_opcode} or @code{fr_fix}.
  1482. If you generate frags separately for the basic insn opcode and any relaxable
  1483. operands, do not call @code{fix_new} thinking you can emit fixups for the
  1484. opcode field from the relaxable frag. It is not guaranteed to be the same frag.
  1485. If you need to emit fixups for the opcode field from inspection of the
  1486. relaxable frag, then you need to generate a common frag for both the basic
  1487. opcode and relaxable fields, or you need to provide the frag for the opcode to
  1488. pass to @code{fix_new}. The latter can be done for example by defining
  1489. @code{TC_FRAG_TYPE} to include a pointer to it and defining @code{TC_FRAG_INIT}
  1490. to set the pointer.
  1491. Sometimes @code{fr_var} is increased instead, and @code{frag_wane} is not
  1492. called. I'm not sure, but I think this is to keep @code{fr_fix} referring to
  1493. an earlier byte, and @code{fr_subtype} set to @code{rs_machine_dependent} so
  1494. that @code{md_convert_frag} will get called.
  1495. @node General relaxing
  1496. @subsection General relaxing
  1497. If using a simple table is not suitable, you may implement arbitrarily complex
  1498. relaxation semantics yourself. For example, the MIPS backend uses this to emit
  1499. different instruction sequences depending upon the size of the symbol being
  1500. accessed.
  1501. When you assemble an instruction that may need relaxation, you should allocate
  1502. a frag using @code{frag_var} or @code{frag_variant} with a type of
  1503. @code{rs_machine_dependent}. You should store some sort of information in the
  1504. @code{fr_subtype} field so that you can figure out what to do with the frag
  1505. later.
  1506. When GAS reaches the end of the input file, it will look through the frags and
  1507. work out their final sizes.
  1508. GAS will first call @code{md_estimate_size_before_relax} on each
  1509. @code{rs_machine_dependent} frag. This function must return an estimated size
  1510. for the frag.
  1511. GAS will then loop over the frags, calling @code{md_relax_frag} on each
  1512. @code{rs_machine_dependent} frag. This function should return the change in
  1513. size of the frag. GAS will keep looping over the frags until none of the frags
  1514. changes size.
  1515. @node Broken words
  1516. @section Broken words
  1517. @cindex internals, broken words
  1518. @cindex broken words
  1519. Some compilers, including GCC, will sometimes emit switch tables specifying
  1520. 16-bit @code{.word} displacements to branch targets, and branch instructions
  1521. that load entries from that table to compute the target address. If this is
  1522. done on a 32-bit machine, there is a chance (at least with really large
  1523. functions) that the displacement will not fit in 16 bits. The assembler
  1524. handles this using a concept called @dfn{broken words}. This idea is well
  1525. named, since there is an implied promise that the 16-bit field will in fact
  1526. hold the specified displacement.
  1527. If broken word processing is enabled, and a situation like this is encountered,
  1528. the assembler will insert a jump instruction into the instruction stream, close
  1529. enough to be reached with the 16-bit displacement. This jump instruction will
  1530. transfer to the real desired target address. Thus, as long as the @code{.word}
  1531. value really is used as a displacement to compute an address to jump to, the
  1532. net effect will be correct (minus a very small efficiency cost). If
  1533. @code{.word} directives with label differences for values are used for other
  1534. purposes, however, things may not work properly. For targets which use broken
  1535. words, the @samp{-K} option will warn when a broken word is discovered.
  1536. The broken word code is turned off by the @code{WORKING_DOT_WORD} macro. It
  1537. isn't needed if @code{.word} emits a value large enough to contain an address
  1538. (or, more correctly, any possible difference between two addresses).
  1539. @node Internal functions
  1540. @section Internal functions
  1541. This section describes basic internal functions used by GAS.
  1542. @menu
  1543. * Warning and error messages:: Warning and error messages
  1544. * Hash tables:: Hash tables
  1545. @end menu
  1546. @node Warning and error messages
  1547. @subsection Warning and error messages
  1548. @deftypefun @{@} int had_warnings (void)
  1549. @deftypefunx @{@} int had_errors (void)
  1550. Returns non-zero if any warnings or errors, respectively, have been printed
  1551. during this invocation.
  1552. @end deftypefun
  1553. @deftypefun @{@} void as_tsktsk (const char *@var{format}, ...)
  1554. @deftypefunx @{@} void as_warn (const char *@var{format}, ...)
  1555. @deftypefunx @{@} void as_bad (const char *@var{format}, ...)
  1556. @deftypefunx @{@} void as_fatal (const char *@var{format}, ...)
  1557. These functions display messages about something amiss with the input file, or
  1558. internal problems in the assembler itself. The current file name and line
  1559. number are printed, followed by the supplied message, formatted using
  1560. @code{vfprintf}, and a final newline.
  1561. An error indicated by @code{as_bad} will result in a non-zero exit status when
  1562. the assembler has finished. Calling @code{as_fatal} will result in immediate
  1563. termination of the assembler process.
  1564. @end deftypefun
  1565. @deftypefun @{@} void as_warn_where (char *@var{file}, unsigned int @var{line}, const char *@var{format}, ...)
  1566. @deftypefunx @{@} void as_bad_where (char *@var{file}, unsigned int @var{line}, const char *@var{format}, ...)
  1567. These variants permit specification of the file name and line number, and are
  1568. used when problems are detected when reprocessing information saved away when
  1569. processing some earlier part of the file. For example, fixups are processed
  1570. after all input has been read, but messages about fixups should refer to the
  1571. original filename and line number that they are applicable to.
  1572. @end deftypefun
  1573. @deftypefun @{@} void sprint_value (char *@var{buf}, valueT @var{val})
  1574. This function is helpful for converting a @code{valueT} value into printable
  1575. format, in case it's wider than modes that @code{*printf} can handle. If the
  1576. type is narrow enough, a decimal number will be produced; otherwise, it will be
  1577. in hexadecimal. The value itself is not examined to make this determination.
  1578. @end deftypefun
  1579. @node Hash tables
  1580. @subsection Hash tables
  1581. @cindex hash tables
  1582. @deftypefun @{@} @{struct hash_control *@} hash_new (void)
  1583. Creates the hash table control structure.
  1584. @end deftypefun
  1585. @deftypefun @{@} void hash_die (struct hash_control *)
  1586. Destroy a hash table.
  1587. @end deftypefun
  1588. @deftypefun @{@} void *hash_delete (struct hash_control *, const char *, int)
  1589. Deletes entry from the hash table, returns the value it had. If the last
  1590. arg is non-zero, free memory allocated for this entry and all entries
  1591. allocated more recently than this entry.
  1592. @end deftypefun
  1593. @deftypefun @{@} void *hash_replace (struct hash_control *, const char *, void *)
  1594. Updates the value for an entry already in the table, returning the old value.
  1595. If no entry was found, just returns NULL.
  1596. @end deftypefun
  1597. @deftypefun @{@} @{const char *@} hash_insert (struct hash_control *, const char *, void *)
  1598. Inserting a value already in the table is an error.
  1599. Returns an error message or NULL.
  1600. @end deftypefun
  1601. @deftypefun @{@} @{const char *@} hash_jam (struct hash_control *, const char *, void *)
  1602. Inserts if the value isn't already present, updates it if it is.
  1603. @end deftypefun
  1604. @node Test suite
  1605. @section Test suite
  1606. @cindex test suite
  1607. The test suite is kind of lame for most processors. Often it only checks to
  1608. see if a couple of files can be assembled without the assembler reporting any
  1609. errors. For more complete testing, write a test which either examines the
  1610. assembler listing, or runs @code{objdump} and examines its output. For the
  1611. latter, the TCL procedure @code{run_dump_test} may come in handy. It takes the
  1612. base name of a file, and looks for @file{@var{file}.d}. This file should
  1613. contain as its initial lines a set of variable settings in @samp{#} comments,
  1614. in the form:
  1615. @example
  1616. #@var{varname}: @var{value}
  1617. @end example
  1618. The @var{varname} may be @code{objdump}, @code{nm}, or @code{as}, in which case
  1619. it specifies the options to be passed to the specified programs. Exactly one
  1620. of @code{objdump} or @code{nm} must be specified, as that also specifies which
  1621. program to run after the assembler has finished. If @var{varname} is
  1622. @code{source}, it specifies the name of the source file; otherwise,
  1623. @file{@var{file}.s} is used. If @var{varname} is @code{name}, it specifies the
  1624. name of the test to be used in the @code{pass} or @code{fail} messages.
  1625. The non-commented parts of the file are interpreted as regular expressions, one
  1626. per line. Blank lines in the @code{objdump} or @code{nm} output are skipped,
  1627. as are blank lines in the @code{.d} file; the other lines are tested to see if
  1628. the regular expression matches the program output. If it does not, the test
  1629. fails.
  1630. Note that this means the tests must be modified if the @code{objdump} output
  1631. style is changed.
  1632. @bye
  1633. @c Local Variables:
  1634. @c fill-column: 79
  1635. @c End: