internals.texi 77 KB

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