dw2gencfi.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524
  1. /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Michal Ludvig <mludvig@suse.cz>
  4. This file is part of GAS, the GNU Assembler.
  5. GAS is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GAS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GAS; see the file COPYING. If not, write to the Free
  15. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. #include "as.h"
  18. #include "dw2gencfi.h"
  19. #include "subsegs.h"
  20. #include "dwarf2dbg.h"
  21. #ifdef TARGET_USE_CFIPOP
  22. /* By default, use difference expressions if DIFF_EXPR_OK is defined. */
  23. #ifndef CFI_DIFF_EXPR_OK
  24. # ifdef DIFF_EXPR_OK
  25. # define CFI_DIFF_EXPR_OK 1
  26. # else
  27. # define CFI_DIFF_EXPR_OK 0
  28. # endif
  29. #endif
  30. #ifndef CFI_DIFF_LSDA_OK
  31. #define CFI_DIFF_LSDA_OK CFI_DIFF_EXPR_OK
  32. #endif
  33. #if CFI_DIFF_EXPR_OK == 1 && CFI_DIFF_LSDA_OK == 0
  34. # error "CFI_DIFF_EXPR_OK should imply CFI_DIFF_LSDA_OK"
  35. #endif
  36. /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
  37. of the CIE. Default to 1 if not otherwise specified. */
  38. #ifndef DWARF2_LINE_MIN_INSN_LENGTH
  39. #define DWARF2_LINE_MIN_INSN_LENGTH 1
  40. #endif
  41. /* By default, use 32-bit relocations from .eh_frame into .text. */
  42. #ifndef DWARF2_FDE_RELOC_SIZE
  43. #define DWARF2_FDE_RELOC_SIZE 4
  44. #endif
  45. /* By default, use a read-only .eh_frame section. */
  46. #ifndef DWARF2_EH_FRAME_READ_ONLY
  47. #define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
  48. #endif
  49. #ifndef EH_FRAME_ALIGNMENT
  50. #define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
  51. #endif
  52. #ifndef tc_cfi_frame_initial_instructions
  53. #define tc_cfi_frame_initial_instructions() ((void)0)
  54. #endif
  55. #ifndef tc_cfi_startproc
  56. # define tc_cfi_startproc() ((void)0)
  57. #endif
  58. #ifndef tc_cfi_endproc
  59. # define tc_cfi_endproc(fde) ((void) (fde))
  60. #endif
  61. #define EH_FRAME_LINKONCE (SUPPORT_FRAME_LINKONCE || compact_eh)
  62. #ifndef DWARF2_FORMAT
  63. #define DWARF2_FORMAT(SEC) dwarf2_format_32bit
  64. #endif
  65. #ifndef DWARF2_ADDR_SIZE
  66. #define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
  67. #endif
  68. #if MULTIPLE_FRAME_SECTIONS
  69. #define CUR_SEG(structp) structp->cur_seg
  70. #define SET_CUR_SEG(structp, seg) structp->cur_seg = seg
  71. #define HANDLED(structp) structp->handled
  72. #define SET_HANDLED(structp, val) structp->handled = val
  73. #else
  74. #define CUR_SEG(structp) NULL
  75. #define SET_CUR_SEG(structp, seg) (void) (0 && seg)
  76. #define HANDLED(structp) 0
  77. #define SET_HANDLED(structp, val) (void) (0 && val)
  78. #endif
  79. #ifndef tc_cfi_reloc_for_encoding
  80. #define tc_cfi_reloc_for_encoding(e) BFD_RELOC_NONE
  81. #endif
  82. /* Private segment collection list. */
  83. struct dwcfi_seg_list
  84. {
  85. segT seg;
  86. int subseg;
  87. char * seg_name;
  88. };
  89. #ifdef SUPPORT_COMPACT_EH
  90. static bfd_boolean compact_eh;
  91. #else
  92. #define compact_eh 0
  93. #endif
  94. static struct hash_control *dwcfi_hash;
  95. /* Emit a single byte into the current segment. */
  96. static inline void
  97. out_one (int byte)
  98. {
  99. FRAG_APPEND_1_CHAR (byte);
  100. }
  101. /* Emit a two-byte word into the current segment. */
  102. static inline void
  103. out_two (int data)
  104. {
  105. md_number_to_chars (frag_more (2), data, 2);
  106. }
  107. /* Emit a four byte word into the current segment. */
  108. static inline void
  109. out_four (int data)
  110. {
  111. md_number_to_chars (frag_more (4), data, 4);
  112. }
  113. /* Emit an unsigned "little-endian base 128" number. */
  114. static void
  115. out_uleb128 (addressT value)
  116. {
  117. output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
  118. }
  119. /* Emit an unsigned "little-endian base 128" number. */
  120. static void
  121. out_sleb128 (offsetT value)
  122. {
  123. output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
  124. }
  125. static offsetT
  126. encoding_size (unsigned char encoding)
  127. {
  128. if (encoding == DW_EH_PE_omit)
  129. return 0;
  130. switch (encoding & 0x7)
  131. {
  132. case 0:
  133. return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
  134. case DW_EH_PE_udata2:
  135. return 2;
  136. case DW_EH_PE_udata4:
  137. return 4;
  138. case DW_EH_PE_udata8:
  139. return 8;
  140. default:
  141. abort ();
  142. }
  143. }
  144. /* Emit expression EXP in ENCODING. If EMIT_ENCODING is true, first
  145. emit a byte containing ENCODING. */
  146. static void
  147. emit_expr_encoded (expressionS *exp, int encoding, bfd_boolean emit_encoding)
  148. {
  149. offsetT size = encoding_size (encoding);
  150. bfd_reloc_code_real_type code;
  151. if (encoding == DW_EH_PE_omit)
  152. return;
  153. if (emit_encoding)
  154. out_one (encoding);
  155. code = tc_cfi_reloc_for_encoding (encoding);
  156. if (code != BFD_RELOC_NONE)
  157. {
  158. reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
  159. char *p = frag_more (size);
  160. md_number_to_chars (p, 0, size);
  161. fix_new (frag_now, p - frag_now->fr_literal, size, exp->X_add_symbol,
  162. exp->X_add_number, howto->pc_relative, code);
  163. }
  164. else if ((encoding & 0x70) == DW_EH_PE_pcrel)
  165. {
  166. #if CFI_DIFF_EXPR_OK
  167. expressionS tmp = *exp;
  168. tmp.X_op = O_subtract;
  169. tmp.X_op_symbol = symbol_temp_new_now ();
  170. emit_expr (&tmp, size);
  171. #elif defined (tc_cfi_emit_pcrel_expr)
  172. tc_cfi_emit_pcrel_expr (exp, size);
  173. #else
  174. abort ();
  175. #endif
  176. }
  177. else
  178. emit_expr (exp, size);
  179. }
  180. /* Build based on segment the derived .debug_...
  181. segment name containing origin segment's postfix name part. */
  182. static char *
  183. get_debugseg_name (segT seg, const char *base_name)
  184. {
  185. const char *name;
  186. if (!seg)
  187. name = "";
  188. else
  189. {
  190. const char * dollar;
  191. const char * dot;
  192. name = bfd_get_section_name (stdoutput, seg);
  193. dollar = strchr (name, '$');
  194. dot = strchr (name + 1, '.');
  195. if (!dollar && !dot)
  196. {
  197. if (!strcmp (base_name, ".eh_frame_entry")
  198. && strcmp (name, ".text") != 0)
  199. return concat (base_name, ".", name, NULL);
  200. name = "";
  201. }
  202. else if (!dollar)
  203. name = dot;
  204. else if (!dot)
  205. name = dollar;
  206. else if (dot < dollar)
  207. name = dot;
  208. else
  209. name = dollar;
  210. }
  211. return concat (base_name, name, NULL);
  212. }
  213. /* Allocate a dwcfi_seg_list structure. */
  214. static struct dwcfi_seg_list *
  215. alloc_debugseg_item (segT seg, int subseg, char *name)
  216. {
  217. struct dwcfi_seg_list *r;
  218. r = (struct dwcfi_seg_list *)
  219. xmalloc (sizeof (struct dwcfi_seg_list) + strlen (name));
  220. r->seg = seg;
  221. r->subseg = subseg;
  222. r->seg_name = name;
  223. return r;
  224. }
  225. static segT
  226. is_now_linkonce_segment (void)
  227. {
  228. if (compact_eh)
  229. return now_seg;
  230. if ((bfd_get_section_flags (stdoutput, now_seg)
  231. & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
  232. | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
  233. | SEC_LINK_DUPLICATES_SAME_CONTENTS)) != 0)
  234. return now_seg;
  235. return NULL;
  236. }
  237. /* Generate debug... segment with same linkonce properties
  238. of based segment. */
  239. static segT
  240. make_debug_seg (segT cseg, char *name, int sflags)
  241. {
  242. segT save_seg = now_seg;
  243. int save_subseg = now_subseg;
  244. segT r;
  245. flagword flags;
  246. r = subseg_new (name, 0);
  247. /* Check if code segment is marked as linked once. */
  248. if (!cseg)
  249. flags = 0;
  250. else
  251. flags = bfd_get_section_flags (stdoutput, cseg)
  252. & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
  253. | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
  254. | SEC_LINK_DUPLICATES_SAME_CONTENTS);
  255. /* Add standard section flags. */
  256. flags |= sflags;
  257. /* Apply possibly linked once flags to new generated segment, too. */
  258. if (!bfd_set_section_flags (stdoutput, r, flags))
  259. as_bad (_("bfd_set_section_flags: %s"),
  260. bfd_errmsg (bfd_get_error ()));
  261. /* Restore to previous segment. */
  262. if (save_seg != NULL)
  263. subseg_set (save_seg, save_subseg);
  264. return r;
  265. }
  266. static void
  267. dwcfi_hash_insert (const char *name, struct dwcfi_seg_list *item)
  268. {
  269. const char *error_string;
  270. if ((error_string = hash_jam (dwcfi_hash, name, (char *) item)))
  271. as_fatal (_("Inserting \"%s\" into structure table failed: %s"),
  272. name, error_string);
  273. }
  274. static struct dwcfi_seg_list *
  275. dwcfi_hash_find (char *name)
  276. {
  277. return (struct dwcfi_seg_list *) hash_find (dwcfi_hash, name);
  278. }
  279. static struct dwcfi_seg_list *
  280. dwcfi_hash_find_or_make (segT cseg, const char *base_name, int flags)
  281. {
  282. struct dwcfi_seg_list *item;
  283. char *name;
  284. /* Initialize dwcfi_hash once. */
  285. if (!dwcfi_hash)
  286. dwcfi_hash = hash_new ();
  287. name = get_debugseg_name (cseg, base_name);
  288. item = dwcfi_hash_find (name);
  289. if (!item)
  290. {
  291. item = alloc_debugseg_item (make_debug_seg (cseg, name, flags), 0, name);
  292. dwcfi_hash_insert (item->seg_name, item);
  293. }
  294. else
  295. free (name);
  296. return item;
  297. }
  298. /* ??? Share this with dwarf2cfg.c. */
  299. #ifndef TC_DWARF2_EMIT_OFFSET
  300. #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
  301. /* Create an offset to .dwarf2_*. */
  302. static void
  303. generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
  304. {
  305. expressionS exp;
  306. exp.X_op = O_symbol;
  307. exp.X_add_symbol = symbol;
  308. exp.X_add_number = 0;
  309. emit_expr (&exp, size);
  310. }
  311. #endif
  312. struct cfi_escape_data
  313. {
  314. struct cfi_escape_data *next;
  315. expressionS exp;
  316. };
  317. struct cie_entry
  318. {
  319. struct cie_entry *next;
  320. #if MULTIPLE_FRAME_SECTIONS
  321. segT cur_seg;
  322. #endif
  323. symbolS *start_address;
  324. unsigned int return_column;
  325. unsigned int signal_frame;
  326. unsigned char fde_encoding;
  327. unsigned char per_encoding;
  328. unsigned char lsda_encoding;
  329. expressionS personality;
  330. struct cfi_insn_data *first, *last;
  331. };
  332. /* List of FDE entries. */
  333. struct fde_entry *all_fde_data;
  334. static struct fde_entry **last_fde_data = &all_fde_data;
  335. /* List of CIEs so that they could be reused. */
  336. static struct cie_entry *cie_root;
  337. /* Stack of old CFI data, for save/restore. */
  338. struct cfa_save_data
  339. {
  340. struct cfa_save_data *next;
  341. offsetT cfa_offset;
  342. };
  343. /* Current open FDE entry. */
  344. struct frch_cfi_data
  345. {
  346. struct fde_entry *cur_fde_data;
  347. symbolS *last_address;
  348. offsetT cur_cfa_offset;
  349. struct cfa_save_data *cfa_save_stack;
  350. };
  351. /* Construct a new FDE structure and add it to the end of the fde list. */
  352. static struct fde_entry *
  353. alloc_fde_entry (void)
  354. {
  355. struct fde_entry *fde = (struct fde_entry *)
  356. xcalloc (1, sizeof (struct fde_entry));
  357. frchain_now->frch_cfi_data = (struct frch_cfi_data *)
  358. xcalloc (1, sizeof (struct frch_cfi_data));
  359. frchain_now->frch_cfi_data->cur_fde_data = fde;
  360. *last_fde_data = fde;
  361. last_fde_data = &fde->next;
  362. SET_CUR_SEG (fde, is_now_linkonce_segment ());
  363. SET_HANDLED (fde, 0);
  364. fde->last = &fde->data;
  365. fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
  366. fde->per_encoding = DW_EH_PE_omit;
  367. fde->lsda_encoding = DW_EH_PE_omit;
  368. fde->eh_header_type = EH_COMPACT_UNKNOWN;
  369. return fde;
  370. }
  371. /* The following functions are available for a backend to construct its
  372. own unwind information, usually from legacy unwind directives. */
  373. /* Construct a new INSN structure and add it to the end of the insn list
  374. for the currently active FDE. */
  375. static bfd_boolean cfi_sections_set = FALSE;
  376. static int cfi_sections = CFI_EMIT_eh_frame;
  377. int all_cfi_sections = 0;
  378. static struct fde_entry *last_fde;
  379. static struct cfi_insn_data *
  380. alloc_cfi_insn_data (void)
  381. {
  382. struct cfi_insn_data *insn = (struct cfi_insn_data *)
  383. xcalloc (1, sizeof (struct cfi_insn_data));
  384. struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
  385. *cur_fde_data->last = insn;
  386. cur_fde_data->last = &insn->next;
  387. SET_CUR_SEG (insn, is_now_linkonce_segment ());
  388. return insn;
  389. }
  390. /* Construct a new FDE structure that begins at LABEL. */
  391. void
  392. cfi_new_fde (symbolS *label)
  393. {
  394. struct fde_entry *fde = alloc_fde_entry ();
  395. fde->start_address = label;
  396. frchain_now->frch_cfi_data->last_address = label;
  397. }
  398. /* End the currently open FDE. */
  399. void
  400. cfi_end_fde (symbolS *label)
  401. {
  402. frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
  403. free (frchain_now->frch_cfi_data);
  404. frchain_now->frch_cfi_data = NULL;
  405. }
  406. /* Set the return column for the current FDE. */
  407. void
  408. cfi_set_return_column (unsigned regno)
  409. {
  410. frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
  411. }
  412. void
  413. cfi_set_sections (void)
  414. {
  415. frchain_now->frch_cfi_data->cur_fde_data->sections = all_cfi_sections;
  416. }
  417. /* Universal functions to store new instructions. */
  418. static void
  419. cfi_add_CFA_insn (int insn)
  420. {
  421. struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
  422. insn_ptr->insn = insn;
  423. }
  424. static void
  425. cfi_add_CFA_insn_reg (int insn, unsigned regno)
  426. {
  427. struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
  428. insn_ptr->insn = insn;
  429. insn_ptr->u.r = regno;
  430. }
  431. static void
  432. cfi_add_CFA_insn_offset (int insn, offsetT offset)
  433. {
  434. struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
  435. insn_ptr->insn = insn;
  436. insn_ptr->u.i = offset;
  437. }
  438. static void
  439. cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
  440. {
  441. struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
  442. insn_ptr->insn = insn;
  443. insn_ptr->u.rr.reg1 = reg1;
  444. insn_ptr->u.rr.reg2 = reg2;
  445. }
  446. static void
  447. cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
  448. {
  449. struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
  450. insn_ptr->insn = insn;
  451. insn_ptr->u.ri.reg = regno;
  452. insn_ptr->u.ri.offset = offset;
  453. }
  454. /* Add a CFI insn to advance the PC from the last address to LABEL. */
  455. void
  456. cfi_add_advance_loc (symbolS *label)
  457. {
  458. struct cfi_insn_data *insn = alloc_cfi_insn_data ();
  459. insn->insn = DW_CFA_advance_loc;
  460. insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
  461. insn->u.ll.lab2 = label;
  462. frchain_now->frch_cfi_data->last_address = label;
  463. }
  464. /* Add a CFI insn to label the current position in the CFI segment. */
  465. void
  466. cfi_add_label (const char *name)
  467. {
  468. unsigned int len = strlen (name) + 1;
  469. struct cfi_insn_data *insn = alloc_cfi_insn_data ();
  470. insn->insn = CFI_label;
  471. obstack_grow (&notes, name, len);
  472. insn->u.sym_name = (char *) obstack_finish (&notes);
  473. }
  474. /* Add a DW_CFA_offset record to the CFI data. */
  475. void
  476. cfi_add_CFA_offset (unsigned regno, offsetT offset)
  477. {
  478. unsigned int abs_data_align;
  479. gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
  480. cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
  481. abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
  482. ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
  483. if (offset % abs_data_align)
  484. as_bad (_("register save offset not a multiple of %u"), abs_data_align);
  485. }
  486. /* Add a DW_CFA_def_cfa record to the CFI data. */
  487. void
  488. cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
  489. {
  490. cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
  491. frchain_now->frch_cfi_data->cur_cfa_offset = offset;
  492. }
  493. /* Add a DW_CFA_register record to the CFI data. */
  494. void
  495. cfi_add_CFA_register (unsigned reg1, unsigned reg2)
  496. {
  497. cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
  498. }
  499. /* Add a DW_CFA_def_cfa_register record to the CFI data. */
  500. void
  501. cfi_add_CFA_def_cfa_register (unsigned regno)
  502. {
  503. cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
  504. }
  505. /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
  506. void
  507. cfi_add_CFA_def_cfa_offset (offsetT offset)
  508. {
  509. cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
  510. frchain_now->frch_cfi_data->cur_cfa_offset = offset;
  511. }
  512. void
  513. cfi_add_CFA_restore (unsigned regno)
  514. {
  515. cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
  516. }
  517. void
  518. cfi_add_CFA_undefined (unsigned regno)
  519. {
  520. cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
  521. }
  522. void
  523. cfi_add_CFA_same_value (unsigned regno)
  524. {
  525. cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
  526. }
  527. void
  528. cfi_add_CFA_remember_state (void)
  529. {
  530. struct cfa_save_data *p;
  531. cfi_add_CFA_insn (DW_CFA_remember_state);
  532. p = (struct cfa_save_data *) xmalloc (sizeof (*p));
  533. p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
  534. p->next = frchain_now->frch_cfi_data->cfa_save_stack;
  535. frchain_now->frch_cfi_data->cfa_save_stack = p;
  536. }
  537. void
  538. cfi_add_CFA_restore_state (void)
  539. {
  540. struct cfa_save_data *p;
  541. cfi_add_CFA_insn (DW_CFA_restore_state);
  542. p = frchain_now->frch_cfi_data->cfa_save_stack;
  543. if (p)
  544. {
  545. frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
  546. frchain_now->frch_cfi_data->cfa_save_stack = p->next;
  547. free (p);
  548. }
  549. else
  550. as_bad (_("CFI state restore without previous remember"));
  551. }
  552. /* Parse CFI assembler directives. */
  553. static void dot_cfi (int);
  554. static void dot_cfi_escape (int);
  555. static void dot_cfi_sections (int);
  556. static void dot_cfi_startproc (int);
  557. static void dot_cfi_endproc (int);
  558. static void dot_cfi_fde_data (int);
  559. static void dot_cfi_personality (int);
  560. static void dot_cfi_personality_id (int);
  561. static void dot_cfi_lsda (int);
  562. static void dot_cfi_val_encoded_addr (int);
  563. static void dot_cfi_inline_lsda (int);
  564. static void dot_cfi_label (int);
  565. const pseudo_typeS cfi_pseudo_table[] =
  566. {
  567. { "cfi_sections", dot_cfi_sections, 0 },
  568. { "cfi_startproc", dot_cfi_startproc, 0 },
  569. { "cfi_endproc", dot_cfi_endproc, 0 },
  570. { "cfi_fde_data", dot_cfi_fde_data, 0 },
  571. { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
  572. { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
  573. { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
  574. { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
  575. { "cfi_offset", dot_cfi, DW_CFA_offset },
  576. { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
  577. { "cfi_register", dot_cfi, DW_CFA_register },
  578. { "cfi_return_column", dot_cfi, CFI_return_column },
  579. { "cfi_restore", dot_cfi, DW_CFA_restore },
  580. { "cfi_undefined", dot_cfi, DW_CFA_undefined },
  581. { "cfi_same_value", dot_cfi, DW_CFA_same_value },
  582. { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
  583. { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
  584. { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
  585. { "cfi_escape", dot_cfi_escape, 0 },
  586. { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
  587. { "cfi_personality", dot_cfi_personality, 0 },
  588. { "cfi_personality_id", dot_cfi_personality_id, 0 },
  589. { "cfi_lsda", dot_cfi_lsda, 0 },
  590. { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 },
  591. { "cfi_inline_lsda", dot_cfi_inline_lsda, 0 },
  592. { "cfi_label", dot_cfi_label, 0 },
  593. { NULL, NULL, 0 }
  594. };
  595. static void
  596. cfi_parse_separator (void)
  597. {
  598. SKIP_WHITESPACE ();
  599. if (*input_line_pointer == ',')
  600. input_line_pointer++;
  601. else
  602. as_bad (_("missing separator"));
  603. }
  604. #ifndef tc_parse_to_dw2regnum
  605. static void
  606. tc_parse_to_dw2regnum (expressionS *exp)
  607. {
  608. # ifdef tc_regname_to_dw2regnum
  609. SKIP_WHITESPACE ();
  610. if (is_name_beginner (*input_line_pointer)
  611. || (*input_line_pointer == '%'
  612. && is_name_beginner (*++input_line_pointer)))
  613. {
  614. char *name, c;
  615. c = get_symbol_name (& name);
  616. exp->X_op = O_constant;
  617. exp->X_add_number = tc_regname_to_dw2regnum (name);
  618. restore_line_pointer (c);
  619. }
  620. else
  621. # endif
  622. expression_and_evaluate (exp);
  623. }
  624. #endif
  625. static unsigned
  626. cfi_parse_reg (void)
  627. {
  628. int regno;
  629. expressionS exp;
  630. tc_parse_to_dw2regnum (&exp);
  631. switch (exp.X_op)
  632. {
  633. case O_register:
  634. case O_constant:
  635. regno = exp.X_add_number;
  636. break;
  637. default:
  638. regno = -1;
  639. break;
  640. }
  641. if (regno < 0)
  642. {
  643. as_bad (_("bad register expression"));
  644. regno = 0;
  645. }
  646. return regno;
  647. }
  648. static offsetT
  649. cfi_parse_const (void)
  650. {
  651. return get_absolute_expression ();
  652. }
  653. static void
  654. dot_cfi (int arg)
  655. {
  656. offsetT offset;
  657. unsigned reg1, reg2;
  658. if (frchain_now->frch_cfi_data == NULL)
  659. {
  660. as_bad (_("CFI instruction used without previous .cfi_startproc"));
  661. ignore_rest_of_line ();
  662. return;
  663. }
  664. /* If the last address was not at the current PC, advance to current. */
  665. if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
  666. || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
  667. != frag_now_fix ())
  668. cfi_add_advance_loc (symbol_temp_new_now ());
  669. switch (arg)
  670. {
  671. case DW_CFA_offset:
  672. reg1 = cfi_parse_reg ();
  673. cfi_parse_separator ();
  674. offset = cfi_parse_const ();
  675. cfi_add_CFA_offset (reg1, offset);
  676. break;
  677. case CFI_rel_offset:
  678. reg1 = cfi_parse_reg ();
  679. cfi_parse_separator ();
  680. offset = cfi_parse_const ();
  681. cfi_add_CFA_offset (reg1,
  682. offset - frchain_now->frch_cfi_data->cur_cfa_offset);
  683. break;
  684. case DW_CFA_def_cfa:
  685. reg1 = cfi_parse_reg ();
  686. cfi_parse_separator ();
  687. offset = cfi_parse_const ();
  688. cfi_add_CFA_def_cfa (reg1, offset);
  689. break;
  690. case DW_CFA_register:
  691. reg1 = cfi_parse_reg ();
  692. cfi_parse_separator ();
  693. reg2 = cfi_parse_reg ();
  694. cfi_add_CFA_register (reg1, reg2);
  695. break;
  696. case DW_CFA_def_cfa_register:
  697. reg1 = cfi_parse_reg ();
  698. cfi_add_CFA_def_cfa_register (reg1);
  699. break;
  700. case DW_CFA_def_cfa_offset:
  701. offset = cfi_parse_const ();
  702. cfi_add_CFA_def_cfa_offset (offset);
  703. break;
  704. case CFI_adjust_cfa_offset:
  705. offset = cfi_parse_const ();
  706. cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
  707. + offset);
  708. break;
  709. case DW_CFA_restore:
  710. for (;;)
  711. {
  712. reg1 = cfi_parse_reg ();
  713. cfi_add_CFA_restore (reg1);
  714. SKIP_WHITESPACE ();
  715. if (*input_line_pointer != ',')
  716. break;
  717. ++input_line_pointer;
  718. }
  719. break;
  720. case DW_CFA_undefined:
  721. for (;;)
  722. {
  723. reg1 = cfi_parse_reg ();
  724. cfi_add_CFA_undefined (reg1);
  725. SKIP_WHITESPACE ();
  726. if (*input_line_pointer != ',')
  727. break;
  728. ++input_line_pointer;
  729. }
  730. break;
  731. case DW_CFA_same_value:
  732. reg1 = cfi_parse_reg ();
  733. cfi_add_CFA_same_value (reg1);
  734. break;
  735. case CFI_return_column:
  736. reg1 = cfi_parse_reg ();
  737. cfi_set_return_column (reg1);
  738. break;
  739. case DW_CFA_remember_state:
  740. cfi_add_CFA_remember_state ();
  741. break;
  742. case DW_CFA_restore_state:
  743. cfi_add_CFA_restore_state ();
  744. break;
  745. case DW_CFA_GNU_window_save:
  746. cfi_add_CFA_insn (DW_CFA_GNU_window_save);
  747. break;
  748. case CFI_signal_frame:
  749. frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
  750. break;
  751. default:
  752. abort ();
  753. }
  754. demand_empty_rest_of_line ();
  755. }
  756. static void
  757. dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
  758. {
  759. struct cfi_escape_data *head, **tail, *e;
  760. struct cfi_insn_data *insn;
  761. if (frchain_now->frch_cfi_data == NULL)
  762. {
  763. as_bad (_("CFI instruction used without previous .cfi_startproc"));
  764. ignore_rest_of_line ();
  765. return;
  766. }
  767. /* If the last address was not at the current PC, advance to current. */
  768. if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
  769. || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
  770. != frag_now_fix ())
  771. cfi_add_advance_loc (symbol_temp_new_now ());
  772. tail = &head;
  773. do
  774. {
  775. e = (struct cfi_escape_data *) xmalloc (sizeof (*e));
  776. do_parse_cons_expression (&e->exp, 1);
  777. *tail = e;
  778. tail = &e->next;
  779. }
  780. while (*input_line_pointer++ == ',');
  781. *tail = NULL;
  782. insn = alloc_cfi_insn_data ();
  783. insn->insn = CFI_escape;
  784. insn->u.esc = head;
  785. --input_line_pointer;
  786. demand_empty_rest_of_line ();
  787. }
  788. static void
  789. dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
  790. {
  791. struct fde_entry *fde;
  792. offsetT encoding;
  793. if (frchain_now->frch_cfi_data == NULL)
  794. {
  795. as_bad (_("CFI instruction used without previous .cfi_startproc"));
  796. ignore_rest_of_line ();
  797. return;
  798. }
  799. fde = frchain_now->frch_cfi_data->cur_fde_data;
  800. encoding = cfi_parse_const ();
  801. if (encoding == DW_EH_PE_omit)
  802. {
  803. demand_empty_rest_of_line ();
  804. fde->per_encoding = encoding;
  805. return;
  806. }
  807. if ((encoding & 0xff) != encoding
  808. || ((((encoding & 0x70) != 0
  809. #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
  810. && (encoding & 0x70) != DW_EH_PE_pcrel
  811. #endif
  812. )
  813. /* leb128 can be handled, but does something actually need it? */
  814. || (encoding & 7) == DW_EH_PE_uleb128
  815. || (encoding & 7) > DW_EH_PE_udata8)
  816. && tc_cfi_reloc_for_encoding (encoding) == BFD_RELOC_NONE))
  817. {
  818. as_bad (_("invalid or unsupported encoding in .cfi_personality"));
  819. ignore_rest_of_line ();
  820. return;
  821. }
  822. if (*input_line_pointer++ != ',')
  823. {
  824. as_bad (_(".cfi_personality requires encoding and symbol arguments"));
  825. ignore_rest_of_line ();
  826. return;
  827. }
  828. expression_and_evaluate (&fde->personality);
  829. switch (fde->personality.X_op)
  830. {
  831. case O_symbol:
  832. break;
  833. case O_constant:
  834. if ((encoding & 0x70) == DW_EH_PE_pcrel)
  835. encoding = DW_EH_PE_omit;
  836. break;
  837. default:
  838. encoding = DW_EH_PE_omit;
  839. break;
  840. }
  841. fde->per_encoding = encoding;
  842. if (encoding == DW_EH_PE_omit)
  843. {
  844. as_bad (_("wrong second argument to .cfi_personality"));
  845. ignore_rest_of_line ();
  846. return;
  847. }
  848. demand_empty_rest_of_line ();
  849. }
  850. static void
  851. dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
  852. {
  853. struct fde_entry *fde;
  854. offsetT encoding;
  855. if (frchain_now->frch_cfi_data == NULL)
  856. {
  857. as_bad (_("CFI instruction used without previous .cfi_startproc"));
  858. ignore_rest_of_line ();
  859. return;
  860. }
  861. fde = frchain_now->frch_cfi_data->cur_fde_data;
  862. encoding = cfi_parse_const ();
  863. if (encoding == DW_EH_PE_omit)
  864. {
  865. demand_empty_rest_of_line ();
  866. fde->lsda_encoding = encoding;
  867. return;
  868. }
  869. if ((encoding & 0xff) != encoding
  870. || ((((encoding & 0x70) != 0
  871. #if CFI_DIFF_LSDA_OK || defined tc_cfi_emit_pcrel_expr
  872. && (encoding & 0x70) != DW_EH_PE_pcrel
  873. #endif
  874. )
  875. /* leb128 can be handled, but does something actually need it? */
  876. || (encoding & 7) == DW_EH_PE_uleb128
  877. || (encoding & 7) > DW_EH_PE_udata8)
  878. && tc_cfi_reloc_for_encoding (encoding) == BFD_RELOC_NONE))
  879. {
  880. as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
  881. ignore_rest_of_line ();
  882. return;
  883. }
  884. if (*input_line_pointer++ != ',')
  885. {
  886. as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
  887. ignore_rest_of_line ();
  888. return;
  889. }
  890. fde->lsda_encoding = encoding;
  891. expression_and_evaluate (&fde->lsda);
  892. switch (fde->lsda.X_op)
  893. {
  894. case O_symbol:
  895. break;
  896. case O_constant:
  897. if ((encoding & 0x70) == DW_EH_PE_pcrel)
  898. encoding = DW_EH_PE_omit;
  899. break;
  900. default:
  901. encoding = DW_EH_PE_omit;
  902. break;
  903. }
  904. fde->lsda_encoding = encoding;
  905. if (encoding == DW_EH_PE_omit)
  906. {
  907. as_bad (_("wrong second argument to .cfi_lsda"));
  908. ignore_rest_of_line ();
  909. return;
  910. }
  911. demand_empty_rest_of_line ();
  912. }
  913. static void
  914. dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
  915. {
  916. struct cfi_insn_data *insn_ptr;
  917. offsetT encoding;
  918. if (frchain_now->frch_cfi_data == NULL)
  919. {
  920. as_bad (_("CFI instruction used without previous .cfi_startproc"));
  921. ignore_rest_of_line ();
  922. return;
  923. }
  924. /* If the last address was not at the current PC, advance to current. */
  925. if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
  926. || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
  927. != frag_now_fix ())
  928. cfi_add_advance_loc (symbol_temp_new_now ());
  929. insn_ptr = alloc_cfi_insn_data ();
  930. insn_ptr->insn = CFI_val_encoded_addr;
  931. insn_ptr->u.ea.reg = cfi_parse_reg ();
  932. cfi_parse_separator ();
  933. encoding = cfi_parse_const ();
  934. if ((encoding & 0xff) != encoding
  935. || ((encoding & 0x70) != 0
  936. #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
  937. && (encoding & 0x70) != DW_EH_PE_pcrel
  938. #endif
  939. )
  940. /* leb128 can be handled, but does something actually need it? */
  941. || (encoding & 7) == DW_EH_PE_uleb128
  942. || (encoding & 7) > DW_EH_PE_udata8)
  943. {
  944. as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
  945. encoding = DW_EH_PE_omit;
  946. }
  947. cfi_parse_separator ();
  948. expression_and_evaluate (&insn_ptr->u.ea.exp);
  949. switch (insn_ptr->u.ea.exp.X_op)
  950. {
  951. case O_symbol:
  952. break;
  953. case O_constant:
  954. if ((encoding & 0x70) != DW_EH_PE_pcrel)
  955. break;
  956. default:
  957. encoding = DW_EH_PE_omit;
  958. break;
  959. }
  960. insn_ptr->u.ea.encoding = encoding;
  961. if (encoding == DW_EH_PE_omit)
  962. {
  963. as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
  964. ignore_rest_of_line ();
  965. return;
  966. }
  967. demand_empty_rest_of_line ();
  968. }
  969. static void
  970. dot_cfi_label (int ignored ATTRIBUTE_UNUSED)
  971. {
  972. char *name = read_symbol_name ();
  973. if (name == NULL)
  974. return;
  975. /* If the last address was not at the current PC, advance to current. */
  976. if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
  977. || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
  978. != frag_now_fix ())
  979. cfi_add_advance_loc (symbol_temp_new_now ());
  980. cfi_add_label (name);
  981. demand_empty_rest_of_line ();
  982. }
  983. static void
  984. dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
  985. {
  986. int sections = 0;
  987. SKIP_WHITESPACE ();
  988. if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
  989. while (1)
  990. {
  991. char * saved_ilp;
  992. char *name, c;
  993. saved_ilp = input_line_pointer;
  994. c = get_symbol_name (& name);
  995. if (strncmp (name, ".eh_frame", sizeof ".eh_frame") == 0
  996. && name[9] != '_')
  997. sections |= CFI_EMIT_eh_frame;
  998. else if (strncmp (name, ".debug_frame", sizeof ".debug_frame") == 0)
  999. sections |= CFI_EMIT_debug_frame;
  1000. #if SUPPORT_COMPACT_EH
  1001. else if (strncmp (name, ".eh_frame_entry", sizeof ".eh_frame_entry") == 0)
  1002. {
  1003. compact_eh = TRUE;
  1004. sections |= CFI_EMIT_eh_frame_compact;
  1005. }
  1006. #endif
  1007. #ifdef tc_cfi_section_name
  1008. else if (strcmp (name, tc_cfi_section_name) == 0)
  1009. sections |= CFI_EMIT_target;
  1010. #endif
  1011. else
  1012. {
  1013. *input_line_pointer = c;
  1014. input_line_pointer = saved_ilp;
  1015. break;
  1016. }
  1017. *input_line_pointer = c;
  1018. SKIP_WHITESPACE_AFTER_NAME ();
  1019. if (*input_line_pointer == ',')
  1020. {
  1021. name = input_line_pointer++;
  1022. SKIP_WHITESPACE ();
  1023. if (!is_name_beginner (*input_line_pointer) && *input_line_pointer != '"')
  1024. {
  1025. input_line_pointer = name;
  1026. break;
  1027. }
  1028. }
  1029. else if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
  1030. break;
  1031. }
  1032. demand_empty_rest_of_line ();
  1033. if (cfi_sections_set && cfi_sections != sections)
  1034. as_bad (_("inconsistent uses of .cfi_sections"));
  1035. cfi_sections_set = TRUE;
  1036. cfi_sections = sections;
  1037. }
  1038. static void
  1039. dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
  1040. {
  1041. int simple = 0;
  1042. if (frchain_now->frch_cfi_data != NULL)
  1043. {
  1044. as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
  1045. ignore_rest_of_line ();
  1046. return;
  1047. }
  1048. cfi_new_fde (symbol_temp_new_now ());
  1049. SKIP_WHITESPACE ();
  1050. if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
  1051. {
  1052. char * saved_ilp = input_line_pointer;
  1053. char *name, c;
  1054. c = get_symbol_name (& name);
  1055. if (strcmp (name, "simple") == 0)
  1056. {
  1057. simple = 1;
  1058. restore_line_pointer (c);
  1059. }
  1060. else
  1061. input_line_pointer = saved_ilp;
  1062. }
  1063. demand_empty_rest_of_line ();
  1064. all_cfi_sections |= cfi_sections;
  1065. cfi_set_sections ();
  1066. frchain_now->frch_cfi_data->cur_cfa_offset = 0;
  1067. if (!simple)
  1068. tc_cfi_frame_initial_instructions ();
  1069. if ((cfi_sections & CFI_EMIT_target) != 0)
  1070. tc_cfi_startproc ();
  1071. }
  1072. static void
  1073. dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
  1074. {
  1075. if (frchain_now->frch_cfi_data == NULL)
  1076. {
  1077. as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
  1078. ignore_rest_of_line ();
  1079. return;
  1080. }
  1081. last_fde = frchain_now->frch_cfi_data->cur_fde_data;
  1082. cfi_end_fde (symbol_temp_new_now ());
  1083. demand_empty_rest_of_line ();
  1084. if ((cfi_sections & CFI_EMIT_target) != 0)
  1085. tc_cfi_endproc (last_fde);
  1086. }
  1087. static segT
  1088. get_cfi_seg (segT cseg, const char *base, flagword flags, int align)
  1089. {
  1090. /* Exclude .debug_frame sections for Compact EH. */
  1091. if (SUPPORT_FRAME_LINKONCE || ((flags & SEC_DEBUGGING) == 0 && compact_eh))
  1092. {
  1093. struct dwcfi_seg_list *l;
  1094. l = dwcfi_hash_find_or_make (cseg, base, flags);
  1095. cseg = l->seg;
  1096. subseg_set (cseg, l->subseg);
  1097. }
  1098. else
  1099. {
  1100. cseg = subseg_new (base, 0);
  1101. bfd_set_section_flags (stdoutput, cseg, flags);
  1102. }
  1103. record_alignment (cseg, align);
  1104. return cseg;
  1105. }
  1106. #if SUPPORT_COMPACT_EH
  1107. static void
  1108. dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
  1109. {
  1110. struct fde_entry *fde;
  1111. if (frchain_now->frch_cfi_data == NULL)
  1112. {
  1113. as_bad (_("CFI instruction used without previous .cfi_startproc"));
  1114. ignore_rest_of_line ();
  1115. return;
  1116. }
  1117. fde = frchain_now->frch_cfi_data->cur_fde_data;
  1118. fde->personality_id = cfi_parse_const ();
  1119. demand_empty_rest_of_line ();
  1120. if (fde->personality_id == 0 || fde->personality_id > 3)
  1121. {
  1122. as_bad (_("wrong argument to .cfi_personality_id"));
  1123. return;
  1124. }
  1125. }
  1126. static void
  1127. dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
  1128. {
  1129. if (frchain_now->frch_cfi_data == NULL)
  1130. {
  1131. as_bad (_(".cfi_fde_data without corresponding .cfi_startproc"));
  1132. ignore_rest_of_line ();
  1133. return;
  1134. }
  1135. last_fde = frchain_now->frch_cfi_data->cur_fde_data;
  1136. if ((cfi_sections & CFI_EMIT_target) != 0
  1137. || (cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
  1138. {
  1139. struct cfi_escape_data *head, **tail, *e;
  1140. int num_ops = 0;
  1141. tail = &head;
  1142. if (!is_it_end_of_statement ())
  1143. {
  1144. num_ops = 0;
  1145. do
  1146. {
  1147. e = (struct cfi_escape_data *) xmalloc (sizeof (*e));
  1148. do_parse_cons_expression (&e->exp, 1);
  1149. *tail = e;
  1150. tail = &e->next;
  1151. num_ops++;
  1152. }
  1153. while (*input_line_pointer++ == ',');
  1154. --input_line_pointer;
  1155. }
  1156. *tail = NULL;
  1157. if (last_fde->lsda_encoding != DW_EH_PE_omit)
  1158. last_fde->eh_header_type = EH_COMPACT_HAS_LSDA;
  1159. else if (num_ops <= 3 && last_fde->per_encoding == DW_EH_PE_omit)
  1160. last_fde->eh_header_type = EH_COMPACT_INLINE;
  1161. else
  1162. last_fde->eh_header_type = EH_COMPACT_OUTLINE;
  1163. if (last_fde->eh_header_type == EH_COMPACT_INLINE)
  1164. num_ops = 3;
  1165. last_fde->eh_data_size = num_ops;
  1166. last_fde->eh_data = (bfd_byte *) xmalloc (num_ops);
  1167. num_ops = 0;
  1168. while (head)
  1169. {
  1170. e = head;
  1171. head = e->next;
  1172. last_fde->eh_data[num_ops++] = e->exp.X_add_number;
  1173. free (e);
  1174. }
  1175. if (last_fde->eh_header_type == EH_COMPACT_INLINE)
  1176. while (num_ops < 3)
  1177. last_fde->eh_data[num_ops++] = tc_compact_eh_opcode_stop;
  1178. }
  1179. demand_empty_rest_of_line ();
  1180. }
  1181. /* Function to emit the compact unwinding opcodes stored in the
  1182. fde's eh_data field. The end of the opcode data will be
  1183. padded to the value in align. */
  1184. static void
  1185. output_compact_unwind_data (struct fde_entry *fde, int align)
  1186. {
  1187. int data_size = fde->eh_data_size + 2;
  1188. int align_padding;
  1189. int amask;
  1190. char *p;
  1191. fde->eh_loc = symbol_temp_new_now ();
  1192. p = frag_more (1);
  1193. if (fde->personality_id != 0)
  1194. *p = fde->personality_id;
  1195. else if (fde->per_encoding != DW_EH_PE_omit)
  1196. {
  1197. *p = 0;
  1198. emit_expr_encoded (&fde->personality, fde->per_encoding, FALSE);
  1199. data_size += encoding_size (fde->per_encoding);
  1200. }
  1201. else
  1202. *p = 1;
  1203. amask = (1 << align) - 1;
  1204. align_padding = ((data_size + amask) & ~amask) - data_size;
  1205. p = frag_more (fde->eh_data_size + 1 + align_padding);
  1206. memcpy (p, fde->eh_data, fde->eh_data_size);
  1207. p += fde->eh_data_size;
  1208. while (align_padding-- > 0)
  1209. *(p++) = tc_compact_eh_opcode_pad;
  1210. *(p++) = tc_compact_eh_opcode_stop;
  1211. fde->eh_header_type = EH_COMPACT_OUTLINE_DONE;
  1212. }
  1213. /* Handle the .cfi_inline_lsda directive. */
  1214. static void
  1215. dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
  1216. {
  1217. segT ccseg;
  1218. int align;
  1219. long max_alignment = 28;
  1220. if (!last_fde)
  1221. {
  1222. as_bad (_("unexpected .cfi_inline_lsda"));
  1223. ignore_rest_of_line ();
  1224. return;
  1225. }
  1226. if ((last_fde->sections & CFI_EMIT_eh_frame_compact) == 0)
  1227. {
  1228. as_bad (_(".cfi_inline_lsda not valid for this frame"));
  1229. ignore_rest_of_line ();
  1230. return;
  1231. }
  1232. if (last_fde->eh_header_type != EH_COMPACT_UNKNOWN
  1233. && last_fde->eh_header_type != EH_COMPACT_HAS_LSDA)
  1234. {
  1235. as_bad (_(".cfi_inline_lsda seen for frame without .cfi_lsda"));
  1236. ignore_rest_of_line ();
  1237. return;
  1238. }
  1239. #ifdef md_flush_pending_output
  1240. md_flush_pending_output ();
  1241. #endif
  1242. align = get_absolute_expression ();
  1243. if (align > max_alignment)
  1244. {
  1245. align = max_alignment;
  1246. as_bad (_("Alignment too large: %d. assumed."), align);
  1247. }
  1248. else if (align < 0)
  1249. {
  1250. as_warn (_("Alignment negative: 0 assumed."));
  1251. align = 0;
  1252. }
  1253. demand_empty_rest_of_line ();
  1254. ccseg = CUR_SEG (last_fde);
  1255. /* Open .gnu_extab section. */
  1256. get_cfi_seg (ccseg, ".gnu_extab",
  1257. (SEC_ALLOC | SEC_LOAD | SEC_DATA
  1258. | DWARF2_EH_FRAME_READ_ONLY),
  1259. 1);
  1260. frag_align (align, 0, 0);
  1261. record_alignment (now_seg, align);
  1262. if (last_fde->eh_header_type == EH_COMPACT_HAS_LSDA)
  1263. output_compact_unwind_data (last_fde, align);
  1264. last_fde = NULL;
  1265. return;
  1266. }
  1267. #else /* !SUPPORT_COMPACT_EH */
  1268. static void
  1269. dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
  1270. {
  1271. as_bad (_(".cfi_inline_lsda is not supported for this target"));
  1272. ignore_rest_of_line ();
  1273. }
  1274. static void
  1275. dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
  1276. {
  1277. as_bad (_(".cfi_fde_data is not supported for this target"));
  1278. ignore_rest_of_line ();
  1279. }
  1280. static void
  1281. dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
  1282. {
  1283. as_bad (_(".cfi_personality_id is not supported for this target"));
  1284. ignore_rest_of_line ();
  1285. }
  1286. #endif
  1287. static void
  1288. output_cfi_insn (struct cfi_insn_data *insn)
  1289. {
  1290. offsetT offset;
  1291. unsigned int regno;
  1292. switch (insn->insn)
  1293. {
  1294. case DW_CFA_advance_loc:
  1295. {
  1296. symbolS *from = insn->u.ll.lab1;
  1297. symbolS *to = insn->u.ll.lab2;
  1298. if (symbol_get_frag (to) == symbol_get_frag (from))
  1299. {
  1300. addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
  1301. addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
  1302. if (scaled <= 0x3F)
  1303. out_one (DW_CFA_advance_loc + scaled);
  1304. else if (scaled <= 0xFF)
  1305. {
  1306. out_one (DW_CFA_advance_loc1);
  1307. out_one (scaled);
  1308. }
  1309. else if (scaled <= 0xFFFF)
  1310. {
  1311. out_one (DW_CFA_advance_loc2);
  1312. out_two (scaled);
  1313. }
  1314. else
  1315. {
  1316. out_one (DW_CFA_advance_loc4);
  1317. out_four (scaled);
  1318. }
  1319. }
  1320. else
  1321. {
  1322. expressionS exp;
  1323. exp.X_op = O_subtract;
  1324. exp.X_add_symbol = to;
  1325. exp.X_op_symbol = from;
  1326. exp.X_add_number = 0;
  1327. /* The code in ehopt.c expects that one byte of the encoding
  1328. is already allocated to the frag. This comes from the way
  1329. that it scans the .eh_frame section looking first for the
  1330. .byte DW_CFA_advance_loc4. */
  1331. *frag_more (1) = DW_CFA_advance_loc4;
  1332. frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
  1333. make_expr_symbol (&exp), frag_now_fix () - 1,
  1334. (char *) frag_now);
  1335. }
  1336. }
  1337. break;
  1338. case DW_CFA_def_cfa:
  1339. offset = insn->u.ri.offset;
  1340. if (offset < 0)
  1341. {
  1342. out_one (DW_CFA_def_cfa_sf);
  1343. out_uleb128 (insn->u.ri.reg);
  1344. out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
  1345. }
  1346. else
  1347. {
  1348. out_one (DW_CFA_def_cfa);
  1349. out_uleb128 (insn->u.ri.reg);
  1350. out_uleb128 (offset);
  1351. }
  1352. break;
  1353. case DW_CFA_def_cfa_register:
  1354. case DW_CFA_undefined:
  1355. case DW_CFA_same_value:
  1356. out_one (insn->insn);
  1357. out_uleb128 (insn->u.r);
  1358. break;
  1359. case DW_CFA_def_cfa_offset:
  1360. offset = insn->u.i;
  1361. if (offset < 0)
  1362. {
  1363. out_one (DW_CFA_def_cfa_offset_sf);
  1364. out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
  1365. }
  1366. else
  1367. {
  1368. out_one (DW_CFA_def_cfa_offset);
  1369. out_uleb128 (offset);
  1370. }
  1371. break;
  1372. case DW_CFA_restore:
  1373. regno = insn->u.r;
  1374. if (regno <= 0x3F)
  1375. {
  1376. out_one (DW_CFA_restore + regno);
  1377. }
  1378. else
  1379. {
  1380. out_one (DW_CFA_restore_extended);
  1381. out_uleb128 (regno);
  1382. }
  1383. break;
  1384. case DW_CFA_offset:
  1385. regno = insn->u.ri.reg;
  1386. offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
  1387. if (offset < 0)
  1388. {
  1389. out_one (DW_CFA_offset_extended_sf);
  1390. out_uleb128 (regno);
  1391. out_sleb128 (offset);
  1392. }
  1393. else if (regno <= 0x3F)
  1394. {
  1395. out_one (DW_CFA_offset + regno);
  1396. out_uleb128 (offset);
  1397. }
  1398. else
  1399. {
  1400. out_one (DW_CFA_offset_extended);
  1401. out_uleb128 (regno);
  1402. out_uleb128 (offset);
  1403. }
  1404. break;
  1405. case DW_CFA_register:
  1406. out_one (DW_CFA_register);
  1407. out_uleb128 (insn->u.rr.reg1);
  1408. out_uleb128 (insn->u.rr.reg2);
  1409. break;
  1410. case DW_CFA_remember_state:
  1411. case DW_CFA_restore_state:
  1412. out_one (insn->insn);
  1413. break;
  1414. case DW_CFA_GNU_window_save:
  1415. out_one (DW_CFA_GNU_window_save);
  1416. break;
  1417. case CFI_escape:
  1418. {
  1419. struct cfi_escape_data *e;
  1420. for (e = insn->u.esc; e ; e = e->next)
  1421. emit_expr (&e->exp, 1);
  1422. break;
  1423. }
  1424. case CFI_val_encoded_addr:
  1425. {
  1426. unsigned encoding = insn->u.ea.encoding;
  1427. offsetT enc_size;
  1428. if (encoding == DW_EH_PE_omit)
  1429. break;
  1430. out_one (DW_CFA_val_expression);
  1431. out_uleb128 (insn->u.ea.reg);
  1432. switch (encoding & 0x7)
  1433. {
  1434. case DW_EH_PE_absptr:
  1435. enc_size = DWARF2_ADDR_SIZE (stdoutput);
  1436. break;
  1437. case DW_EH_PE_udata2:
  1438. enc_size = 2;
  1439. break;
  1440. case DW_EH_PE_udata4:
  1441. enc_size = 4;
  1442. break;
  1443. case DW_EH_PE_udata8:
  1444. enc_size = 8;
  1445. break;
  1446. default:
  1447. abort ();
  1448. }
  1449. /* If the user has requested absolute encoding,
  1450. then use the smaller DW_OP_addr encoding. */
  1451. if (insn->u.ea.encoding == DW_EH_PE_absptr)
  1452. {
  1453. out_uleb128 (1 + enc_size);
  1454. out_one (DW_OP_addr);
  1455. }
  1456. else
  1457. {
  1458. out_uleb128 (1 + 1 + enc_size);
  1459. out_one (DW_OP_GNU_encoded_addr);
  1460. out_one (encoding);
  1461. if ((encoding & 0x70) == DW_EH_PE_pcrel)
  1462. {
  1463. #if CFI_DIFF_EXPR_OK
  1464. insn->u.ea.exp.X_op = O_subtract;
  1465. insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
  1466. #elif defined (tc_cfi_emit_pcrel_expr)
  1467. tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, enc_size);
  1468. break;
  1469. #else
  1470. abort ();
  1471. #endif
  1472. }
  1473. }
  1474. emit_expr (&insn->u.ea.exp, enc_size);
  1475. }
  1476. break;
  1477. case CFI_label:
  1478. colon (insn->u.sym_name);
  1479. break;
  1480. default:
  1481. abort ();
  1482. }
  1483. }
  1484. static void
  1485. output_cie (struct cie_entry *cie, bfd_boolean eh_frame, int align)
  1486. {
  1487. symbolS *after_size_address, *end_address;
  1488. expressionS exp;
  1489. struct cfi_insn_data *i;
  1490. offsetT augmentation_size;
  1491. int enc;
  1492. enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
  1493. cie->start_address = symbol_temp_new_now ();
  1494. after_size_address = symbol_temp_make ();
  1495. end_address = symbol_temp_make ();
  1496. exp.X_op = O_subtract;
  1497. exp.X_add_symbol = end_address;
  1498. exp.X_op_symbol = after_size_address;
  1499. exp.X_add_number = 0;
  1500. if (eh_frame || fmt == dwarf2_format_32bit)
  1501. emit_expr (&exp, 4); /* Length. */
  1502. else
  1503. {
  1504. if (fmt == dwarf2_format_64bit)
  1505. out_four (-1);
  1506. emit_expr (&exp, 8); /* Length. */
  1507. }
  1508. symbol_set_value_now (after_size_address);
  1509. if (eh_frame)
  1510. out_four (0); /* CIE id. */
  1511. else
  1512. {
  1513. out_four (-1); /* CIE id. */
  1514. if (fmt != dwarf2_format_32bit)
  1515. out_four (-1);
  1516. }
  1517. out_one (DW_CIE_VERSION); /* Version. */
  1518. if (eh_frame)
  1519. {
  1520. out_one ('z'); /* Augmentation. */
  1521. if (cie->per_encoding != DW_EH_PE_omit)
  1522. out_one ('P');
  1523. if (cie->lsda_encoding != DW_EH_PE_omit)
  1524. out_one ('L');
  1525. out_one ('R');
  1526. }
  1527. if (cie->signal_frame)
  1528. out_one ('S');
  1529. out_one (0);
  1530. out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
  1531. out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
  1532. if (DW_CIE_VERSION == 1) /* Return column. */
  1533. out_one (cie->return_column);
  1534. else
  1535. out_uleb128 (cie->return_column);
  1536. if (eh_frame)
  1537. {
  1538. augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
  1539. if (cie->per_encoding != DW_EH_PE_omit)
  1540. augmentation_size += 1 + encoding_size (cie->per_encoding);
  1541. out_uleb128 (augmentation_size); /* Augmentation size. */
  1542. emit_expr_encoded (&cie->personality, cie->per_encoding, TRUE);
  1543. if (cie->lsda_encoding != DW_EH_PE_omit)
  1544. out_one (cie->lsda_encoding);
  1545. }
  1546. switch (DWARF2_FDE_RELOC_SIZE)
  1547. {
  1548. case 2:
  1549. enc = DW_EH_PE_sdata2;
  1550. break;
  1551. case 4:
  1552. enc = DW_EH_PE_sdata4;
  1553. break;
  1554. case 8:
  1555. enc = DW_EH_PE_sdata8;
  1556. break;
  1557. default:
  1558. abort ();
  1559. }
  1560. #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
  1561. enc |= DW_EH_PE_pcrel;
  1562. #endif
  1563. #ifdef DWARF2_FDE_RELOC_ENCODING
  1564. /* Allow target to override encoding. */
  1565. enc = DWARF2_FDE_RELOC_ENCODING (enc);
  1566. #endif
  1567. cie->fde_encoding = enc;
  1568. if (eh_frame)
  1569. out_one (enc);
  1570. if (cie->first)
  1571. {
  1572. for (i = cie->first; i != cie->last; i = i->next)
  1573. {
  1574. if (CUR_SEG (i) != CUR_SEG (cie))
  1575. continue;
  1576. output_cfi_insn (i);
  1577. }
  1578. }
  1579. frag_align (align, DW_CFA_nop, 0);
  1580. symbol_set_value_now (end_address);
  1581. }
  1582. static void
  1583. output_fde (struct fde_entry *fde, struct cie_entry *cie,
  1584. bfd_boolean eh_frame, struct cfi_insn_data *first,
  1585. int align)
  1586. {
  1587. symbolS *after_size_address, *end_address;
  1588. expressionS exp;
  1589. offsetT augmentation_size;
  1590. enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
  1591. int offset_size;
  1592. int addr_size;
  1593. after_size_address = symbol_temp_make ();
  1594. end_address = symbol_temp_make ();
  1595. exp.X_op = O_subtract;
  1596. exp.X_add_symbol = end_address;
  1597. exp.X_op_symbol = after_size_address;
  1598. exp.X_add_number = 0;
  1599. if (eh_frame || fmt == dwarf2_format_32bit)
  1600. offset_size = 4;
  1601. else
  1602. {
  1603. if (fmt == dwarf2_format_64bit)
  1604. out_four (-1);
  1605. offset_size = 8;
  1606. }
  1607. emit_expr (&exp, offset_size); /* Length. */
  1608. symbol_set_value_now (after_size_address);
  1609. if (eh_frame)
  1610. {
  1611. exp.X_op = O_subtract;
  1612. exp.X_add_symbol = after_size_address;
  1613. exp.X_op_symbol = cie->start_address;
  1614. exp.X_add_number = 0;
  1615. emit_expr (&exp, offset_size); /* CIE offset. */
  1616. }
  1617. else
  1618. {
  1619. TC_DWARF2_EMIT_OFFSET (cie->start_address, offset_size);
  1620. }
  1621. exp.X_op = O_symbol;
  1622. if (eh_frame)
  1623. {
  1624. bfd_reloc_code_real_type code
  1625. = tc_cfi_reloc_for_encoding (cie->fde_encoding);
  1626. if (code != BFD_RELOC_NONE)
  1627. {
  1628. reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
  1629. char *p = frag_more (4);
  1630. md_number_to_chars (p, 0, 4);
  1631. fix_new (frag_now, p - frag_now->fr_literal, 4, fde->start_address,
  1632. 0, howto->pc_relative, code);
  1633. }
  1634. else
  1635. {
  1636. exp.X_op = O_subtract;
  1637. exp.X_add_number = 0;
  1638. #if CFI_DIFF_EXPR_OK
  1639. exp.X_add_symbol = fde->start_address;
  1640. exp.X_op_symbol = symbol_temp_new_now ();
  1641. emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
  1642. #else
  1643. exp.X_op = O_symbol;
  1644. exp.X_add_symbol = fde->start_address;
  1645. #if defined(tc_cfi_emit_pcrel_expr)
  1646. tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
  1647. #else
  1648. emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
  1649. #endif
  1650. #endif
  1651. }
  1652. addr_size = DWARF2_FDE_RELOC_SIZE;
  1653. }
  1654. else
  1655. {
  1656. exp.X_add_number = 0;
  1657. exp.X_add_symbol = fde->start_address;
  1658. addr_size = DWARF2_ADDR_SIZE (stdoutput);
  1659. emit_expr (&exp, addr_size);
  1660. }
  1661. exp.X_op = O_subtract;
  1662. exp.X_add_symbol = fde->end_address;
  1663. exp.X_op_symbol = fde->start_address; /* Code length. */
  1664. exp.X_add_number = 0;
  1665. emit_expr (&exp, addr_size);
  1666. augmentation_size = encoding_size (fde->lsda_encoding);
  1667. if (eh_frame)
  1668. out_uleb128 (augmentation_size); /* Augmentation size. */
  1669. emit_expr_encoded (&fde->lsda, cie->lsda_encoding, FALSE);
  1670. for (; first; first = first->next)
  1671. if (CUR_SEG (first) == CUR_SEG (fde))
  1672. output_cfi_insn (first);
  1673. frag_align (align, DW_CFA_nop, 0);
  1674. symbol_set_value_now (end_address);
  1675. }
  1676. static struct cie_entry *
  1677. select_cie_for_fde (struct fde_entry *fde, bfd_boolean eh_frame,
  1678. struct cfi_insn_data **pfirst, int align)
  1679. {
  1680. struct cfi_insn_data *i, *j;
  1681. struct cie_entry *cie;
  1682. for (cie = cie_root; cie; cie = cie->next)
  1683. {
  1684. if (CUR_SEG (cie) != CUR_SEG (fde))
  1685. continue;
  1686. if (cie->return_column != fde->return_column
  1687. || cie->signal_frame != fde->signal_frame
  1688. || cie->per_encoding != fde->per_encoding
  1689. || cie->lsda_encoding != fde->lsda_encoding)
  1690. continue;
  1691. if (cie->per_encoding != DW_EH_PE_omit)
  1692. {
  1693. if (cie->personality.X_op != fde->personality.X_op
  1694. || cie->personality.X_add_number
  1695. != fde->personality.X_add_number)
  1696. continue;
  1697. switch (cie->personality.X_op)
  1698. {
  1699. case O_constant:
  1700. if (cie->personality.X_unsigned != fde->personality.X_unsigned)
  1701. continue;
  1702. break;
  1703. case O_symbol:
  1704. if (cie->personality.X_add_symbol
  1705. != fde->personality.X_add_symbol)
  1706. continue;
  1707. break;
  1708. default:
  1709. abort ();
  1710. }
  1711. }
  1712. for (i = cie->first, j = fde->data;
  1713. i != cie->last && j != NULL;
  1714. i = i->next, j = j->next)
  1715. {
  1716. if (i->insn != j->insn)
  1717. goto fail;
  1718. switch (i->insn)
  1719. {
  1720. case DW_CFA_advance_loc:
  1721. case DW_CFA_remember_state:
  1722. /* We reached the first advance/remember in the FDE,
  1723. but did not reach the end of the CIE list. */
  1724. goto fail;
  1725. case DW_CFA_offset:
  1726. case DW_CFA_def_cfa:
  1727. if (i->u.ri.reg != j->u.ri.reg)
  1728. goto fail;
  1729. if (i->u.ri.offset != j->u.ri.offset)
  1730. goto fail;
  1731. break;
  1732. case DW_CFA_register:
  1733. if (i->u.rr.reg1 != j->u.rr.reg1)
  1734. goto fail;
  1735. if (i->u.rr.reg2 != j->u.rr.reg2)
  1736. goto fail;
  1737. break;
  1738. case DW_CFA_def_cfa_register:
  1739. case DW_CFA_restore:
  1740. case DW_CFA_undefined:
  1741. case DW_CFA_same_value:
  1742. if (i->u.r != j->u.r)
  1743. goto fail;
  1744. break;
  1745. case DW_CFA_def_cfa_offset:
  1746. if (i->u.i != j->u.i)
  1747. goto fail;
  1748. break;
  1749. case CFI_escape:
  1750. case CFI_val_encoded_addr:
  1751. case CFI_label:
  1752. /* Don't bother matching these for now. */
  1753. goto fail;
  1754. default:
  1755. abort ();
  1756. }
  1757. }
  1758. /* Success if we reached the end of the CIE list, and we've either
  1759. run out of FDE entries or we've encountered an advance,
  1760. remember, or escape. */
  1761. if (i == cie->last
  1762. && (!j
  1763. || j->insn == DW_CFA_advance_loc
  1764. || j->insn == DW_CFA_remember_state
  1765. || j->insn == CFI_escape
  1766. || j->insn == CFI_val_encoded_addr
  1767. || j->insn == CFI_label))
  1768. {
  1769. *pfirst = j;
  1770. return cie;
  1771. }
  1772. fail:;
  1773. }
  1774. cie = (struct cie_entry *) xmalloc (sizeof (struct cie_entry));
  1775. cie->next = cie_root;
  1776. cie_root = cie;
  1777. SET_CUR_SEG (cie, CUR_SEG (fde));
  1778. cie->return_column = fde->return_column;
  1779. cie->signal_frame = fde->signal_frame;
  1780. cie->per_encoding = fde->per_encoding;
  1781. cie->lsda_encoding = fde->lsda_encoding;
  1782. cie->personality = fde->personality;
  1783. cie->first = fde->data;
  1784. for (i = cie->first; i ; i = i->next)
  1785. if (i->insn == DW_CFA_advance_loc
  1786. || i->insn == DW_CFA_remember_state
  1787. || i->insn == CFI_escape
  1788. || i->insn == CFI_val_encoded_addr
  1789. || i->insn == CFI_label)
  1790. break;
  1791. cie->last = i;
  1792. *pfirst = i;
  1793. output_cie (cie, eh_frame, align);
  1794. return cie;
  1795. }
  1796. #ifdef md_reg_eh_frame_to_debug_frame
  1797. static void
  1798. cfi_change_reg_numbers (struct cfi_insn_data *insn, segT ccseg)
  1799. {
  1800. for (; insn; insn = insn->next)
  1801. {
  1802. if (CUR_SEG (insn) != ccseg)
  1803. continue;
  1804. switch (insn->insn)
  1805. {
  1806. case DW_CFA_advance_loc:
  1807. case DW_CFA_def_cfa_offset:
  1808. case DW_CFA_remember_state:
  1809. case DW_CFA_restore_state:
  1810. case DW_CFA_GNU_window_save:
  1811. case CFI_escape:
  1812. case CFI_label:
  1813. break;
  1814. case DW_CFA_def_cfa:
  1815. case DW_CFA_offset:
  1816. insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
  1817. break;
  1818. case DW_CFA_def_cfa_register:
  1819. case DW_CFA_undefined:
  1820. case DW_CFA_same_value:
  1821. case DW_CFA_restore:
  1822. insn->u.r = md_reg_eh_frame_to_debug_frame (insn->u.r);
  1823. break;
  1824. case DW_CFA_register:
  1825. insn->u.rr.reg1 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg1);
  1826. insn->u.rr.reg2 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg2);
  1827. break;
  1828. case CFI_val_encoded_addr:
  1829. insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
  1830. break;
  1831. default:
  1832. abort ();
  1833. }
  1834. }
  1835. }
  1836. #else
  1837. #define cfi_change_reg_numbers(insn, cseg) do { } while (0)
  1838. #endif
  1839. #if SUPPORT_COMPACT_EH
  1840. static void
  1841. cfi_emit_eh_header (symbolS *sym, bfd_vma addend)
  1842. {
  1843. expressionS exp;
  1844. exp.X_add_number = addend;
  1845. exp.X_add_symbol = sym;
  1846. emit_expr_encoded (&exp, DW_EH_PE_sdata4 | DW_EH_PE_pcrel, FALSE);
  1847. }
  1848. static void
  1849. output_eh_header (struct fde_entry *fde)
  1850. {
  1851. char *p;
  1852. bfd_vma addend;
  1853. if (fde->eh_header_type == EH_COMPACT_INLINE)
  1854. addend = 0;
  1855. else
  1856. addend = 1;
  1857. cfi_emit_eh_header (fde->start_address, addend);
  1858. if (fde->eh_header_type == EH_COMPACT_INLINE)
  1859. {
  1860. p = frag_more (4);
  1861. /* Inline entries always use PR1. */
  1862. *(p++) = 1;
  1863. memcpy(p, fde->eh_data, 3);
  1864. }
  1865. else
  1866. {
  1867. if (fde->eh_header_type == EH_COMPACT_LEGACY)
  1868. addend = 1;
  1869. else if (fde->eh_header_type == EH_COMPACT_OUTLINE
  1870. || fde->eh_header_type == EH_COMPACT_OUTLINE_DONE)
  1871. addend = 0;
  1872. else
  1873. abort ();
  1874. cfi_emit_eh_header (fde->eh_loc, addend);
  1875. }
  1876. }
  1877. #endif
  1878. void
  1879. cfi_finish (void)
  1880. {
  1881. struct cie_entry *cie, *cie_next;
  1882. segT cfi_seg, ccseg;
  1883. struct fde_entry *fde;
  1884. struct cfi_insn_data *first;
  1885. int save_flag_traditional_format, seek_next_seg;
  1886. if (all_fde_data == 0)
  1887. return;
  1888. if ((all_cfi_sections & CFI_EMIT_eh_frame) != 0
  1889. || (all_cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
  1890. {
  1891. /* Make sure check_eh_frame doesn't do anything with our output. */
  1892. save_flag_traditional_format = flag_traditional_format;
  1893. flag_traditional_format = 1;
  1894. if (!EH_FRAME_LINKONCE)
  1895. {
  1896. /* Open .eh_frame section. */
  1897. cfi_seg = get_cfi_seg (NULL, ".eh_frame",
  1898. (SEC_ALLOC | SEC_LOAD | SEC_DATA
  1899. | DWARF2_EH_FRAME_READ_ONLY),
  1900. EH_FRAME_ALIGNMENT);
  1901. #ifdef md_fix_up_eh_frame
  1902. md_fix_up_eh_frame (cfi_seg);
  1903. #else
  1904. (void) cfi_seg;
  1905. #endif
  1906. }
  1907. do
  1908. {
  1909. ccseg = NULL;
  1910. seek_next_seg = 0;
  1911. for (cie = cie_root; cie; cie = cie_next)
  1912. {
  1913. cie_next = cie->next;
  1914. free ((void *) cie);
  1915. }
  1916. cie_root = NULL;
  1917. for (fde = all_fde_data; fde ; fde = fde->next)
  1918. {
  1919. if ((fde->sections & CFI_EMIT_eh_frame) == 0
  1920. && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
  1921. continue;
  1922. #if SUPPORT_COMPACT_EH
  1923. /* Emit a LEGACY format header if we have processed all
  1924. of the .cfi directives without encountering either inline or
  1925. out-of-line compact unwinding opcodes. */
  1926. if (fde->eh_header_type == EH_COMPACT_HAS_LSDA
  1927. || fde->eh_header_type == EH_COMPACT_UNKNOWN)
  1928. fde->eh_header_type = EH_COMPACT_LEGACY;
  1929. if (fde->eh_header_type != EH_COMPACT_LEGACY)
  1930. continue;
  1931. #endif
  1932. if (EH_FRAME_LINKONCE)
  1933. {
  1934. if (HANDLED (fde))
  1935. continue;
  1936. if (seek_next_seg && CUR_SEG (fde) != ccseg)
  1937. {
  1938. seek_next_seg = 2;
  1939. continue;
  1940. }
  1941. if (!seek_next_seg)
  1942. {
  1943. ccseg = CUR_SEG (fde);
  1944. /* Open .eh_frame section. */
  1945. cfi_seg = get_cfi_seg (ccseg, ".eh_frame",
  1946. (SEC_ALLOC | SEC_LOAD | SEC_DATA
  1947. | DWARF2_EH_FRAME_READ_ONLY),
  1948. EH_FRAME_ALIGNMENT);
  1949. #ifdef md_fix_up_eh_frame
  1950. md_fix_up_eh_frame (cfi_seg);
  1951. #else
  1952. (void) cfi_seg;
  1953. #endif
  1954. seek_next_seg = 1;
  1955. }
  1956. SET_HANDLED (fde, 1);
  1957. }
  1958. if (fde->end_address == NULL)
  1959. {
  1960. as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
  1961. fde->end_address = fde->start_address;
  1962. }
  1963. cie = select_cie_for_fde (fde, TRUE, &first, 2);
  1964. fde->eh_loc = symbol_temp_new_now ();
  1965. output_fde (fde, cie, TRUE, first,
  1966. fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
  1967. }
  1968. }
  1969. while (EH_FRAME_LINKONCE && seek_next_seg == 2);
  1970. if (EH_FRAME_LINKONCE)
  1971. for (fde = all_fde_data; fde ; fde = fde->next)
  1972. SET_HANDLED (fde, 0);
  1973. #if SUPPORT_COMPACT_EH
  1974. if (compact_eh)
  1975. {
  1976. /* Create remaining out of line table entries. */
  1977. do
  1978. {
  1979. ccseg = NULL;
  1980. seek_next_seg = 0;
  1981. for (fde = all_fde_data; fde ; fde = fde->next)
  1982. {
  1983. if ((fde->sections & CFI_EMIT_eh_frame) == 0
  1984. && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
  1985. continue;
  1986. if (fde->eh_header_type != EH_COMPACT_OUTLINE)
  1987. continue;
  1988. if (HANDLED (fde))
  1989. continue;
  1990. if (seek_next_seg && CUR_SEG (fde) != ccseg)
  1991. {
  1992. seek_next_seg = 2;
  1993. continue;
  1994. }
  1995. if (!seek_next_seg)
  1996. {
  1997. ccseg = CUR_SEG (fde);
  1998. /* Open .gnu_extab section. */
  1999. get_cfi_seg (ccseg, ".gnu_extab",
  2000. (SEC_ALLOC | SEC_LOAD | SEC_DATA
  2001. | DWARF2_EH_FRAME_READ_ONLY),
  2002. 1);
  2003. seek_next_seg = 1;
  2004. }
  2005. SET_HANDLED (fde, 1);
  2006. frag_align (1, 0, 0);
  2007. record_alignment (now_seg, 1);
  2008. output_compact_unwind_data (fde, 1);
  2009. }
  2010. }
  2011. while (EH_FRAME_LINKONCE && seek_next_seg == 2);
  2012. for (fde = all_fde_data; fde ; fde = fde->next)
  2013. SET_HANDLED (fde, 0);
  2014. /* Create index table fragments. */
  2015. do
  2016. {
  2017. ccseg = NULL;
  2018. seek_next_seg = 0;
  2019. for (fde = all_fde_data; fde ; fde = fde->next)
  2020. {
  2021. if ((fde->sections & CFI_EMIT_eh_frame) == 0
  2022. && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
  2023. continue;
  2024. if (HANDLED (fde))
  2025. continue;
  2026. if (seek_next_seg && CUR_SEG (fde) != ccseg)
  2027. {
  2028. seek_next_seg = 2;
  2029. continue;
  2030. }
  2031. if (!seek_next_seg)
  2032. {
  2033. ccseg = CUR_SEG (fde);
  2034. /* Open .eh_frame_entry section. */
  2035. cfi_seg = get_cfi_seg (ccseg, ".eh_frame_entry",
  2036. (SEC_ALLOC | SEC_LOAD | SEC_DATA
  2037. | DWARF2_EH_FRAME_READ_ONLY),
  2038. 2);
  2039. seek_next_seg = 1;
  2040. }
  2041. SET_HANDLED (fde, 1);
  2042. output_eh_header (fde);
  2043. }
  2044. }
  2045. while (seek_next_seg == 2);
  2046. for (fde = all_fde_data; fde ; fde = fde->next)
  2047. SET_HANDLED (fde, 0);
  2048. }
  2049. #endif /* SUPPORT_COMPACT_EH */
  2050. flag_traditional_format = save_flag_traditional_format;
  2051. }
  2052. if ((all_cfi_sections & CFI_EMIT_debug_frame) != 0)
  2053. {
  2054. int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
  2055. if (!SUPPORT_FRAME_LINKONCE)
  2056. get_cfi_seg (NULL, ".debug_frame",
  2057. SEC_READONLY | SEC_DEBUGGING,
  2058. alignment);
  2059. do
  2060. {
  2061. ccseg = NULL;
  2062. seek_next_seg = 0;
  2063. for (cie = cie_root; cie; cie = cie_next)
  2064. {
  2065. cie_next = cie->next;
  2066. free ((void *) cie);
  2067. }
  2068. cie_root = NULL;
  2069. for (fde = all_fde_data; fde ; fde = fde->next)
  2070. {
  2071. if ((fde->sections & CFI_EMIT_debug_frame) == 0)
  2072. continue;
  2073. if (SUPPORT_FRAME_LINKONCE)
  2074. {
  2075. if (HANDLED (fde))
  2076. continue;
  2077. if (seek_next_seg && CUR_SEG (fde) != ccseg)
  2078. {
  2079. seek_next_seg = 2;
  2080. continue;
  2081. }
  2082. if (!seek_next_seg)
  2083. {
  2084. ccseg = CUR_SEG (fde);
  2085. /* Open .debug_frame section. */
  2086. get_cfi_seg (ccseg, ".debug_frame",
  2087. SEC_READONLY | SEC_DEBUGGING,
  2088. alignment);
  2089. seek_next_seg = 1;
  2090. }
  2091. SET_HANDLED (fde, 1);
  2092. }
  2093. if (fde->end_address == NULL)
  2094. {
  2095. as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
  2096. fde->end_address = fde->start_address;
  2097. }
  2098. fde->per_encoding = DW_EH_PE_omit;
  2099. fde->lsda_encoding = DW_EH_PE_omit;
  2100. cfi_change_reg_numbers (fde->data, ccseg);
  2101. cie = select_cie_for_fde (fde, FALSE, &first, alignment);
  2102. output_fde (fde, cie, FALSE, first, alignment);
  2103. }
  2104. }
  2105. while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
  2106. if (SUPPORT_FRAME_LINKONCE)
  2107. for (fde = all_fde_data; fde ; fde = fde->next)
  2108. SET_HANDLED (fde, 0);
  2109. }
  2110. }
  2111. #else /* TARGET_USE_CFIPOP */
  2112. /* Emit an intelligible error message for missing support. */
  2113. static void
  2114. dot_cfi_dummy (int ignored ATTRIBUTE_UNUSED)
  2115. {
  2116. as_bad (_("CFI is not supported for this target"));
  2117. ignore_rest_of_line ();
  2118. }
  2119. const pseudo_typeS cfi_pseudo_table[] =
  2120. {
  2121. { "cfi_sections", dot_cfi_dummy, 0 },
  2122. { "cfi_startproc", dot_cfi_dummy, 0 },
  2123. { "cfi_endproc", dot_cfi_dummy, 0 },
  2124. { "cfi_fde_data", dot_cfi_dummy, 0 },
  2125. { "cfi_def_cfa", dot_cfi_dummy, 0 },
  2126. { "cfi_def_cfa_register", dot_cfi_dummy, 0 },
  2127. { "cfi_def_cfa_offset", dot_cfi_dummy, 0 },
  2128. { "cfi_adjust_cfa_offset", dot_cfi_dummy, 0 },
  2129. { "cfi_offset", dot_cfi_dummy, 0 },
  2130. { "cfi_rel_offset", dot_cfi_dummy, 0 },
  2131. { "cfi_register", dot_cfi_dummy, 0 },
  2132. { "cfi_return_column", dot_cfi_dummy, 0 },
  2133. { "cfi_restore", dot_cfi_dummy, 0 },
  2134. { "cfi_undefined", dot_cfi_dummy, 0 },
  2135. { "cfi_same_value", dot_cfi_dummy, 0 },
  2136. { "cfi_remember_state", dot_cfi_dummy, 0 },
  2137. { "cfi_restore_state", dot_cfi_dummy, 0 },
  2138. { "cfi_window_save", dot_cfi_dummy, 0 },
  2139. { "cfi_escape", dot_cfi_dummy, 0 },
  2140. { "cfi_signal_frame", dot_cfi_dummy, 0 },
  2141. { "cfi_personality", dot_cfi_dummy, 0 },
  2142. { "cfi_personality_id", dot_cfi_dummy, 0 },
  2143. { "cfi_lsda", dot_cfi_dummy, 0 },
  2144. { "cfi_val_encoded_addr", dot_cfi_dummy, 0 },
  2145. { "cfi_label", dot_cfi_dummy, 0 },
  2146. { "cfi_inline_lsda", dot_cfi_dummy, 0 },
  2147. { NULL, NULL, 0 }
  2148. };
  2149. void
  2150. cfi_finish (void)
  2151. {
  2152. }
  2153. #endif /* TARGET_USE_CFIPOP */