0033-objtool-Add-ORC-unwind-table-generation.patch 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. From a5a66a79aadeb0459a3a28adf04d41c42e053436 Mon Sep 17 00:00:00 2001
  2. From: Josh Poimboeuf <[email protected]>
  3. Date: Tue, 11 Jul 2017 10:33:42 -0500
  4. Subject: [PATCH 033/242] objtool: Add ORC unwind table generation
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. Now that objtool knows the states of all registers on the stack for each
  10. instruction, it's straightforward to generate debuginfo for an unwinder
  11. to use.
  12. Instead of generating DWARF, generate a new format called ORC, which is
  13. more suitable for an in-kernel unwinder. See
  14. Documentation/x86/orc-unwinder.txt for a more detailed description of
  15. this new debuginfo format and why it's preferable to DWARF.
  16. Signed-off-by: Josh Poimboeuf <[email protected]>
  17. Cc: Andy Lutomirski <[email protected]>
  18. Cc: Borislav Petkov <[email protected]>
  19. Cc: Brian Gerst <[email protected]>
  20. Cc: Denys Vlasenko <[email protected]>
  21. Cc: H. Peter Anvin <[email protected]>
  22. Cc: Jiri Slaby <[email protected]>
  23. Cc: Linus Torvalds <[email protected]>
  24. Cc: Mike Galbraith <[email protected]>
  25. Cc: Peter Zijlstra <[email protected]>
  26. Cc: Thomas Gleixner <[email protected]>
  27. Cc: [email protected]
  28. Link: http://lkml.kernel.org/r/c9b9f01ba6c5ed2bdc9bb0957b78167fdbf9632e.1499786555.git.jpoimboe@redhat.com
  29. Signed-off-by: Ingo Molnar <[email protected]>
  30. (cherry picked from commit 627fce14809ba5610b0cb476cd0186d3fcedecfc)
  31. Signed-off-by: Andy Whitcroft <[email protected]>
  32. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  33. (cherry picked from commit 9460f7766786ad0f8330f78f22b81842632a5398)
  34. Signed-off-by: Fabian Grünbichler <[email protected]>
  35. ---
  36. tools/objtool/Documentation/stack-validation.txt | 56 ++----
  37. tools/objtool/builtin.h | 1 +
  38. tools/objtool/check.h | 15 +-
  39. tools/objtool/elf.h | 15 +-
  40. tools/objtool/orc.h | 30 ++++
  41. tools/objtool/orc_types.h | 85 +++++++++
  42. tools/objtool/builtin-check.c | 2 +-
  43. tools/objtool/builtin-orc.c | 70 ++++++++
  44. tools/objtool/check.c | 58 +++++-
  45. tools/objtool/elf.c | 212 ++++++++++++++++++++--
  46. tools/objtool/objtool.c | 3 +-
  47. tools/objtool/orc_dump.c | 212 ++++++++++++++++++++++
  48. tools/objtool/orc_gen.c | 214 +++++++++++++++++++++++
  49. tools/objtool/Build | 3 +
  50. 14 files changed, 916 insertions(+), 60 deletions(-)
  51. create mode 100644 tools/objtool/orc.h
  52. create mode 100644 tools/objtool/orc_types.h
  53. create mode 100644 tools/objtool/builtin-orc.c
  54. create mode 100644 tools/objtool/orc_dump.c
  55. create mode 100644 tools/objtool/orc_gen.c
  56. diff --git a/tools/objtool/Documentation/stack-validation.txt b/tools/objtool/Documentation/stack-validation.txt
  57. index 17c1195f11f4..6a1af43862df 100644
  58. --- a/tools/objtool/Documentation/stack-validation.txt
  59. +++ b/tools/objtool/Documentation/stack-validation.txt
  60. @@ -11,9 +11,6 @@ analyzes every .o file and ensures the validity of its stack metadata.
  61. It enforces a set of rules on asm code and C inline assembly code so
  62. that stack traces can be reliable.
  63. -Currently it only checks frame pointer usage, but there are plans to add
  64. -CFI validation for C files and CFI generation for asm files.
  65. -
  66. For each function, it recursively follows all possible code paths and
  67. validates the correct frame pointer state at each instruction.
  68. @@ -23,6 +20,10 @@ alternative execution paths to a given instruction (or set of
  69. instructions). Similarly, it knows how to follow switch statements, for
  70. which gcc sometimes uses jump tables.
  71. +(Objtool also has an 'orc generate' subcommand which generates debuginfo
  72. +for the ORC unwinder. See Documentation/x86/orc-unwinder.txt in the
  73. +kernel tree for more details.)
  74. +
  75. Why do we need stack metadata validation?
  76. -----------------------------------------
  77. @@ -93,37 +94,14 @@ a) More reliable stack traces for frame pointer enabled kernels
  78. or at the very end of the function after the stack frame has been
  79. destroyed. This is an inherent limitation of frame pointers.
  80. -b) 100% reliable stack traces for DWARF enabled kernels
  81. -
  82. - (NOTE: This is not yet implemented)
  83. -
  84. - As an alternative to frame pointers, DWARF Call Frame Information
  85. - (CFI) metadata can be used to walk the stack. Unlike frame pointers,
  86. - CFI metadata is out of band. So it doesn't affect runtime
  87. - performance and it can be reliable even when interrupts or exceptions
  88. - are involved.
  89. -
  90. - For C code, gcc automatically generates DWARF CFI metadata. But for
  91. - asm code, generating CFI is a tedious manual approach which requires
  92. - manually placed .cfi assembler macros to be scattered throughout the
  93. - code. It's clumsy and very easy to get wrong, and it makes the real
  94. - code harder to read.
  95. -
  96. - Stacktool will improve this situation in several ways. For code
  97. - which already has CFI annotations, it will validate them. For code
  98. - which doesn't have CFI annotations, it will generate them. So an
  99. - architecture can opt to strip out all the manual .cfi annotations
  100. - from their asm code and have objtool generate them instead.
  101. +b) ORC (Oops Rewind Capability) unwind table generation
  102. - We might also add a runtime stack validation debug option where we
  103. - periodically walk the stack from schedule() and/or an NMI to ensure
  104. - that the stack metadata is sane and that we reach the bottom of the
  105. - stack.
  106. + An alternative to frame pointers and DWARF, ORC unwind data can be
  107. + used to walk the stack. Unlike frame pointers, ORC data is out of
  108. + band. So it doesn't affect runtime performance and it can be
  109. + reliable even when interrupts or exceptions are involved.
  110. - So the benefit of objtool here will be that external tooling should
  111. - always show perfect stack traces. And the same will be true for
  112. - kernel warning/oops traces if the architecture has a runtime DWARF
  113. - unwinder.
  114. + For more details, see Documentation/x86/orc-unwinder.txt.
  115. c) Higher live patching compatibility rate
  116. @@ -211,7 +189,7 @@ they mean, and suggestions for how to fix them.
  117. function, add proper frame pointer logic using the FRAME_BEGIN and
  118. FRAME_END macros. Otherwise, if it's not a callable function, remove
  119. its ELF function annotation by changing ENDPROC to END, and instead
  120. - use the manual CFI hint macros in asm/undwarf.h.
  121. + use the manual unwind hint macros in asm/unwind_hints.h.
  122. If it's a GCC-compiled .c file, the error may be because the function
  123. uses an inline asm() statement which has a "call" instruction. An
  124. @@ -231,8 +209,8 @@ they mean, and suggestions for how to fix them.
  125. If the error is for an asm file, and the instruction is inside (or
  126. reachable from) a callable function, the function should be annotated
  127. with the ENTRY/ENDPROC macros (ENDPROC is the important one).
  128. - Otherwise, the code should probably be annotated with the CFI hint
  129. - macros in asm/undwarf.h so objtool and the unwinder can know the
  130. + Otherwise, the code should probably be annotated with the unwind hint
  131. + macros in asm/unwind_hints.h so objtool and the unwinder can know the
  132. stack state associated with the code.
  133. If you're 100% sure the code won't affect stack traces, or if you're
  134. @@ -258,7 +236,7 @@ they mean, and suggestions for how to fix them.
  135. instructions aren't allowed in a callable function, and are most
  136. likely part of the kernel entry code. They should usually not have
  137. the callable function annotation (ENDPROC) and should always be
  138. - annotated with the CFI hint macros in asm/undwarf.h.
  139. + annotated with the unwind hint macros in asm/unwind_hints.h.
  140. 6. file.o: warning: objtool: func()+0x26: sibling call from callable instruction with modified stack frame
  141. @@ -272,7 +250,7 @@ they mean, and suggestions for how to fix them.
  142. If the instruction is not actually in a callable function (e.g.
  143. kernel entry code), change ENDPROC to END and annotate manually with
  144. - the CFI hint macros in asm/undwarf.h.
  145. + the unwind hint macros in asm/unwind_hints.h.
  146. 7. file: warning: objtool: func()+0x5c: stack state mismatch
  147. @@ -288,8 +266,8 @@ they mean, and suggestions for how to fix them.
  148. Another possibility is that the code has some asm or inline asm which
  149. does some unusual things to the stack or the frame pointer. In such
  150. - cases it's probably appropriate to use the CFI hint macros in
  151. - asm/undwarf.h.
  152. + cases it's probably appropriate to use the unwind hint macros in
  153. + asm/unwind_hints.h.
  154. 8. file.o: warning: objtool: funcA() falls through to next function funcB()
  155. diff --git a/tools/objtool/builtin.h b/tools/objtool/builtin.h
  156. index 34d2ba78a616..dd526067fed5 100644
  157. --- a/tools/objtool/builtin.h
  158. +++ b/tools/objtool/builtin.h
  159. @@ -18,5 +18,6 @@
  160. #define _BUILTIN_H
  161. extern int cmd_check(int argc, const char **argv);
  162. +extern int cmd_orc(int argc, const char **argv);
  163. #endif /* _BUILTIN_H */
  164. diff --git a/tools/objtool/check.h b/tools/objtool/check.h
  165. index da85f5b00ec6..046874bbe226 100644
  166. --- a/tools/objtool/check.h
  167. +++ b/tools/objtool/check.h
  168. @@ -22,12 +22,14 @@
  169. #include "elf.h"
  170. #include "cfi.h"
  171. #include "arch.h"
  172. +#include "orc.h"
  173. #include <linux/hashtable.h>
  174. struct insn_state {
  175. struct cfi_reg cfa;
  176. struct cfi_reg regs[CFI_NUM_REGS];
  177. int stack_size;
  178. + unsigned char type;
  179. bool bp_scratch;
  180. bool drap;
  181. int drap_reg;
  182. @@ -48,6 +50,7 @@ struct instruction {
  183. struct symbol *func;
  184. struct stack_op stack_op;
  185. struct insn_state state;
  186. + struct orc_entry orc;
  187. };
  188. struct objtool_file {
  189. @@ -58,9 +61,19 @@ struct objtool_file {
  190. bool ignore_unreachables, c_file;
  191. };
  192. -int check(const char *objname, bool nofp);
  193. +int check(const char *objname, bool nofp, bool orc);
  194. +
  195. +struct instruction *find_insn(struct objtool_file *file,
  196. + struct section *sec, unsigned long offset);
  197. #define for_each_insn(file, insn) \
  198. list_for_each_entry(insn, &file->insn_list, list)
  199. +#define sec_for_each_insn(file, sec, insn) \
  200. + for (insn = find_insn(file, sec, 0); \
  201. + insn && &insn->list != &file->insn_list && \
  202. + insn->sec == sec; \
  203. + insn = list_next_entry(insn, list))
  204. +
  205. +
  206. #endif /* _CHECK_H */
  207. diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
  208. index 343968b778cb..d86e2ff14466 100644
  209. --- a/tools/objtool/elf.h
  210. +++ b/tools/objtool/elf.h
  211. @@ -28,6 +28,13 @@
  212. # define elf_getshdrstrndx elf_getshstrndx
  213. #endif
  214. +/*
  215. + * Fallback for systems without this "read, mmaping if possible" cmd.
  216. + */
  217. +#ifndef ELF_C_READ_MMAP
  218. +#define ELF_C_READ_MMAP ELF_C_READ
  219. +#endif
  220. +
  221. struct section {
  222. struct list_head list;
  223. GElf_Shdr sh;
  224. @@ -41,6 +48,7 @@ struct section {
  225. char *name;
  226. int idx;
  227. unsigned int len;
  228. + bool changed, text;
  229. };
  230. struct symbol {
  231. @@ -75,7 +83,7 @@ struct elf {
  232. };
  233. -struct elf *elf_open(const char *name);
  234. +struct elf *elf_open(const char *name, int flags);
  235. struct section *find_section_by_name(struct elf *elf, const char *name);
  236. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
  237. struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
  238. @@ -83,6 +91,11 @@ struct rela *find_rela_by_dest(struct section *sec, unsigned long offset);
  239. struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
  240. unsigned int len);
  241. struct symbol *find_containing_func(struct section *sec, unsigned long offset);
  242. +struct section *elf_create_section(struct elf *elf, const char *name, size_t
  243. + entsize, int nr);
  244. +struct section *elf_create_rela_section(struct elf *elf, struct section *base);
  245. +int elf_rebuild_rela_section(struct section *sec);
  246. +int elf_write(struct elf *elf);
  247. void elf_close(struct elf *elf);
  248. #define for_each_sec(file, sec) \
  249. diff --git a/tools/objtool/orc.h b/tools/objtool/orc.h
  250. new file mode 100644
  251. index 000000000000..a4139e386ef3
  252. --- /dev/null
  253. +++ b/tools/objtool/orc.h
  254. @@ -0,0 +1,30 @@
  255. +/*
  256. + * Copyright (C) 2017 Josh Poimboeuf <[email protected]>
  257. + *
  258. + * This program is free software; you can redistribute it and/or
  259. + * modify it under the terms of the GNU General Public License
  260. + * as published by the Free Software Foundation; either version 2
  261. + * of the License, or (at your option) any later version.
  262. + *
  263. + * This program is distributed in the hope that it will be useful,
  264. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  265. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  266. + * GNU General Public License for more details.
  267. + *
  268. + * You should have received a copy of the GNU General Public License
  269. + * along with this program; if not, see <http://www.gnu.org/licenses/>.
  270. + */
  271. +
  272. +#ifndef _ORC_H
  273. +#define _ORC_H
  274. +
  275. +#include "orc_types.h"
  276. +
  277. +struct objtool_file;
  278. +
  279. +int create_orc(struct objtool_file *file);
  280. +int create_orc_sections(struct objtool_file *file);
  281. +
  282. +int orc_dump(const char *objname);
  283. +
  284. +#endif /* _ORC_H */
  285. diff --git a/tools/objtool/orc_types.h b/tools/objtool/orc_types.h
  286. new file mode 100644
  287. index 000000000000..fc5cf6cffd9a
  288. --- /dev/null
  289. +++ b/tools/objtool/orc_types.h
  290. @@ -0,0 +1,85 @@
  291. +/*
  292. + * Copyright (C) 2017 Josh Poimboeuf <[email protected]>
  293. + *
  294. + * This program is free software; you can redistribute it and/or
  295. + * modify it under the terms of the GNU General Public License
  296. + * as published by the Free Software Foundation; either version 2
  297. + * of the License, or (at your option) any later version.
  298. + *
  299. + * This program is distributed in the hope that it will be useful,
  300. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  301. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  302. + * GNU General Public License for more details.
  303. + *
  304. + * You should have received a copy of the GNU General Public License
  305. + * along with this program; if not, see <http://www.gnu.org/licenses/>.
  306. + */
  307. +
  308. +#ifndef _ORC_TYPES_H
  309. +#define _ORC_TYPES_H
  310. +
  311. +#include <linux/types.h>
  312. +#include <linux/compiler.h>
  313. +
  314. +/*
  315. + * The ORC_REG_* registers are base registers which are used to find other
  316. + * registers on the stack.
  317. + *
  318. + * ORC_REG_PREV_SP, also known as DWARF Call Frame Address (CFA), is the
  319. + * address of the previous frame: the caller's SP before it called the current
  320. + * function.
  321. + *
  322. + * ORC_REG_UNDEFINED means the corresponding register's value didn't change in
  323. + * the current frame.
  324. + *
  325. + * The most commonly used base registers are SP and BP -- which the previous SP
  326. + * is usually based on -- and PREV_SP and UNDEFINED -- which the previous BP is
  327. + * usually based on.
  328. + *
  329. + * The rest of the base registers are needed for special cases like entry code
  330. + * and GCC realigned stacks.
  331. + */
  332. +#define ORC_REG_UNDEFINED 0
  333. +#define ORC_REG_PREV_SP 1
  334. +#define ORC_REG_DX 2
  335. +#define ORC_REG_DI 3
  336. +#define ORC_REG_BP 4
  337. +#define ORC_REG_SP 5
  338. +#define ORC_REG_R10 6
  339. +#define ORC_REG_R13 7
  340. +#define ORC_REG_BP_INDIRECT 8
  341. +#define ORC_REG_SP_INDIRECT 9
  342. +#define ORC_REG_MAX 15
  343. +
  344. +/*
  345. + * ORC_TYPE_CALL: Indicates that sp_reg+sp_offset resolves to PREV_SP (the
  346. + * caller's SP right before it made the call). Used for all callable
  347. + * functions, i.e. all C code and all callable asm functions.
  348. + *
  349. + * ORC_TYPE_REGS: Used in entry code to indicate that sp_reg+sp_offset points
  350. + * to a fully populated pt_regs from a syscall, interrupt, or exception.
  351. + *
  352. + * ORC_TYPE_REGS_IRET: Used in entry code to indicate that sp_reg+sp_offset
  353. + * points to the iret return frame.
  354. + */
  355. +#define ORC_TYPE_CALL 0
  356. +#define ORC_TYPE_REGS 1
  357. +#define ORC_TYPE_REGS_IRET 2
  358. +
  359. +/*
  360. + * This struct is more or less a vastly simplified version of the DWARF Call
  361. + * Frame Information standard. It contains only the necessary parts of DWARF
  362. + * CFI, simplified for ease of access by the in-kernel unwinder. It tells the
  363. + * unwinder how to find the previous SP and BP (and sometimes entry regs) on
  364. + * the stack for a given code address. Each instance of the struct corresponds
  365. + * to one or more code locations.
  366. + */
  367. +struct orc_entry {
  368. + s16 sp_offset;
  369. + s16 bp_offset;
  370. + unsigned sp_reg:4;
  371. + unsigned bp_reg:4;
  372. + unsigned type:2;
  373. +} __packed;
  374. +
  375. +#endif /* _ORC_TYPES_H */
  376. diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
  377. index 365c34ecab26..eedf089b1495 100644
  378. --- a/tools/objtool/builtin-check.c
  379. +++ b/tools/objtool/builtin-check.c
  380. @@ -52,5 +52,5 @@ int cmd_check(int argc, const char **argv)
  381. objname = argv[0];
  382. - return check(objname, nofp);
  383. + return check(objname, nofp, false);
  384. }
  385. diff --git a/tools/objtool/builtin-orc.c b/tools/objtool/builtin-orc.c
  386. new file mode 100644
  387. index 000000000000..5ca41ab0df48
  388. --- /dev/null
  389. +++ b/tools/objtool/builtin-orc.c
  390. @@ -0,0 +1,70 @@
  391. +/*
  392. + * Copyright (C) 2017 Josh Poimboeuf <[email protected]>
  393. + *
  394. + * This program is free software; you can redistribute it and/or
  395. + * modify it under the terms of the GNU General Public License
  396. + * as published by the Free Software Foundation; either version 2
  397. + * of the License, or (at your option) any later version.
  398. + *
  399. + * This program is distributed in the hope that it will be useful,
  400. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  401. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  402. + * GNU General Public License for more details.
  403. + *
  404. + * You should have received a copy of the GNU General Public License
  405. + * along with this program; if not, see <http://www.gnu.org/licenses/>.
  406. + */
  407. +
  408. +/*
  409. + * objtool orc:
  410. + *
  411. + * This command analyzes a .o file and adds .orc_unwind and .orc_unwind_ip
  412. + * sections to it, which is used by the in-kernel ORC unwinder.
  413. + *
  414. + * This command is a superset of "objtool check".
  415. + */
  416. +
  417. +#include <string.h>
  418. +#include <subcmd/parse-options.h>
  419. +#include "builtin.h"
  420. +#include "check.h"
  421. +
  422. +
  423. +static const char *orc_usage[] = {
  424. + "objtool orc generate [<options>] file.o",
  425. + "objtool orc dump file.o",
  426. + NULL,
  427. +};
  428. +
  429. +extern const struct option check_options[];
  430. +extern bool nofp;
  431. +
  432. +int cmd_orc(int argc, const char **argv)
  433. +{
  434. + const char *objname;
  435. +
  436. + argc--; argv++;
  437. + if (!strncmp(argv[0], "gen", 3)) {
  438. + argc = parse_options(argc, argv, check_options, orc_usage, 0);
  439. + if (argc != 1)
  440. + usage_with_options(orc_usage, check_options);
  441. +
  442. + objname = argv[0];
  443. +
  444. + return check(objname, nofp, true);
  445. +
  446. + }
  447. +
  448. + if (!strcmp(argv[0], "dump")) {
  449. + if (argc != 2)
  450. + usage_with_options(orc_usage, check_options);
  451. +
  452. + objname = argv[1];
  453. +
  454. + return orc_dump(objname);
  455. + }
  456. +
  457. + usage_with_options(orc_usage, check_options);
  458. +
  459. + return 0;
  460. +}
  461. diff --git a/tools/objtool/check.c b/tools/objtool/check.c
  462. index 2c6d74880403..cb57c526ba17 100644
  463. --- a/tools/objtool/check.c
  464. +++ b/tools/objtool/check.c
  465. @@ -36,8 +36,8 @@ const char *objname;
  466. static bool nofp;
  467. struct cfi_state initial_func_cfi;
  468. -static struct instruction *find_insn(struct objtool_file *file,
  469. - struct section *sec, unsigned long offset)
  470. +struct instruction *find_insn(struct objtool_file *file,
  471. + struct section *sec, unsigned long offset)
  472. {
  473. struct instruction *insn;
  474. @@ -259,6 +259,11 @@ static int decode_instructions(struct objtool_file *file)
  475. if (!(sec->sh.sh_flags & SHF_EXECINSTR))
  476. continue;
  477. + if (strcmp(sec->name, ".altinstr_replacement") &&
  478. + strcmp(sec->name, ".altinstr_aux") &&
  479. + strncmp(sec->name, ".discard.", 9))
  480. + sec->text = true;
  481. +
  482. for (offset = 0; offset < sec->len; offset += insn->len) {
  483. insn = malloc(sizeof(*insn));
  484. if (!insn) {
  485. @@ -947,6 +952,30 @@ static bool has_valid_stack_frame(struct insn_state *state)
  486. return false;
  487. }
  488. +static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
  489. +{
  490. + struct cfi_reg *cfa = &state->cfa;
  491. + struct stack_op *op = &insn->stack_op;
  492. +
  493. + if (cfa->base != CFI_SP)
  494. + return 0;
  495. +
  496. + /* push */
  497. + if (op->dest.type == OP_DEST_PUSH)
  498. + cfa->offset += 8;
  499. +
  500. + /* pop */
  501. + if (op->src.type == OP_SRC_POP)
  502. + cfa->offset -= 8;
  503. +
  504. + /* add immediate to sp */
  505. + if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
  506. + op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
  507. + cfa->offset -= op->src.offset;
  508. +
  509. + return 0;
  510. +}
  511. +
  512. static void save_reg(struct insn_state *state, unsigned char reg, int base,
  513. int offset)
  514. {
  515. @@ -1032,6 +1061,9 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
  516. return 0;
  517. }
  518. + if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
  519. + return update_insn_state_regs(insn, state);
  520. +
  521. switch (op->dest.type) {
  522. case OP_DEST_REG:
  523. @@ -1323,6 +1355,10 @@ static bool insn_state_match(struct instruction *insn, struct insn_state *state)
  524. break;
  525. }
  526. + } else if (state1->type != state2->type) {
  527. + WARN_FUNC("stack state mismatch: type1=%d type2=%d",
  528. + insn->sec, insn->offset, state1->type, state2->type);
  529. +
  530. } else if (state1->drap != state2->drap ||
  531. (state1->drap && state1->drap_reg != state2->drap_reg)) {
  532. WARN_FUNC("stack state mismatch: drap1=%d(%d) drap2=%d(%d)",
  533. @@ -1613,7 +1649,7 @@ static void cleanup(struct objtool_file *file)
  534. elf_close(file->elf);
  535. }
  536. -int check(const char *_objname, bool _nofp)
  537. +int check(const char *_objname, bool _nofp, bool orc)
  538. {
  539. struct objtool_file file;
  540. int ret, warnings = 0;
  541. @@ -1621,7 +1657,7 @@ int check(const char *_objname, bool _nofp)
  542. objname = _objname;
  543. nofp = _nofp;
  544. - file.elf = elf_open(objname);
  545. + file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
  546. if (!file.elf)
  547. return 1;
  548. @@ -1654,6 +1690,20 @@ int check(const char *_objname, bool _nofp)
  549. warnings += ret;
  550. }
  551. + if (orc) {
  552. + ret = create_orc(&file);
  553. + if (ret < 0)
  554. + goto out;
  555. +
  556. + ret = create_orc_sections(&file);
  557. + if (ret < 0)
  558. + goto out;
  559. +
  560. + ret = elf_write(file.elf);
  561. + if (ret < 0)
  562. + goto out;
  563. + }
  564. +
  565. out:
  566. cleanup(&file);
  567. diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
  568. index 1a7e8aa2af58..6e9f980a7d26 100644
  569. --- a/tools/objtool/elf.c
  570. +++ b/tools/objtool/elf.c
  571. @@ -30,16 +30,6 @@
  572. #include "elf.h"
  573. #include "warn.h"
  574. -/*
  575. - * Fallback for systems without this "read, mmaping if possible" cmd.
  576. - */
  577. -#ifndef ELF_C_READ_MMAP
  578. -#define ELF_C_READ_MMAP ELF_C_READ
  579. -#endif
  580. -
  581. -#define WARN_ELF(format, ...) \
  582. - WARN(format ": %s", ##__VA_ARGS__, elf_errmsg(-1))
  583. -
  584. struct section *find_section_by_name(struct elf *elf, const char *name)
  585. {
  586. struct section *sec;
  587. @@ -349,9 +339,10 @@ static int read_relas(struct elf *elf)
  588. return 0;
  589. }
  590. -struct elf *elf_open(const char *name)
  591. +struct elf *elf_open(const char *name, int flags)
  592. {
  593. struct elf *elf;
  594. + Elf_Cmd cmd;
  595. elf_version(EV_CURRENT);
  596. @@ -364,13 +355,20 @@ struct elf *elf_open(const char *name)
  597. INIT_LIST_HEAD(&elf->sections);
  598. - elf->fd = open(name, O_RDONLY);
  599. + elf->fd = open(name, flags);
  600. if (elf->fd == -1) {
  601. perror("open");
  602. goto err;
  603. }
  604. - elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
  605. + if ((flags & O_ACCMODE) == O_RDONLY)
  606. + cmd = ELF_C_READ_MMAP;
  607. + else if ((flags & O_ACCMODE) == O_RDWR)
  608. + cmd = ELF_C_RDWR;
  609. + else /* O_WRONLY */
  610. + cmd = ELF_C_WRITE;
  611. +
  612. + elf->elf = elf_begin(elf->fd, cmd, NULL);
  613. if (!elf->elf) {
  614. WARN_ELF("elf_begin");
  615. goto err;
  616. @@ -397,6 +395,194 @@ struct elf *elf_open(const char *name)
  617. return NULL;
  618. }
  619. +struct section *elf_create_section(struct elf *elf, const char *name,
  620. + size_t entsize, int nr)
  621. +{
  622. + struct section *sec, *shstrtab;
  623. + size_t size = entsize * nr;
  624. + struct Elf_Scn *s;
  625. + Elf_Data *data;
  626. +
  627. + sec = malloc(sizeof(*sec));
  628. + if (!sec) {
  629. + perror("malloc");
  630. + return NULL;
  631. + }
  632. + memset(sec, 0, sizeof(*sec));
  633. +
  634. + INIT_LIST_HEAD(&sec->symbol_list);
  635. + INIT_LIST_HEAD(&sec->rela_list);
  636. + hash_init(sec->rela_hash);
  637. + hash_init(sec->symbol_hash);
  638. +
  639. + list_add_tail(&sec->list, &elf->sections);
  640. +
  641. + s = elf_newscn(elf->elf);
  642. + if (!s) {
  643. + WARN_ELF("elf_newscn");
  644. + return NULL;
  645. + }
  646. +
  647. + sec->name = strdup(name);
  648. + if (!sec->name) {
  649. + perror("strdup");
  650. + return NULL;
  651. + }
  652. +
  653. + sec->idx = elf_ndxscn(s);
  654. + sec->len = size;
  655. + sec->changed = true;
  656. +
  657. + sec->data = elf_newdata(s);
  658. + if (!sec->data) {
  659. + WARN_ELF("elf_newdata");
  660. + return NULL;
  661. + }
  662. +
  663. + sec->data->d_size = size;
  664. + sec->data->d_align = 1;
  665. +
  666. + if (size) {
  667. + sec->data->d_buf = malloc(size);
  668. + if (!sec->data->d_buf) {
  669. + perror("malloc");
  670. + return NULL;
  671. + }
  672. + memset(sec->data->d_buf, 0, size);
  673. + }
  674. +
  675. + if (!gelf_getshdr(s, &sec->sh)) {
  676. + WARN_ELF("gelf_getshdr");
  677. + return NULL;
  678. + }
  679. +
  680. + sec->sh.sh_size = size;
  681. + sec->sh.sh_entsize = entsize;
  682. + sec->sh.sh_type = SHT_PROGBITS;
  683. + sec->sh.sh_addralign = 1;
  684. + sec->sh.sh_flags = SHF_ALLOC;
  685. +
  686. +
  687. + /* Add section name to .shstrtab */
  688. + shstrtab = find_section_by_name(elf, ".shstrtab");
  689. + if (!shstrtab) {
  690. + WARN("can't find .shstrtab section");
  691. + return NULL;
  692. + }
  693. +
  694. + s = elf_getscn(elf->elf, shstrtab->idx);
  695. + if (!s) {
  696. + WARN_ELF("elf_getscn");
  697. + return NULL;
  698. + }
  699. +
  700. + data = elf_newdata(s);
  701. + if (!data) {
  702. + WARN_ELF("elf_newdata");
  703. + return NULL;
  704. + }
  705. +
  706. + data->d_buf = sec->name;
  707. + data->d_size = strlen(name) + 1;
  708. + data->d_align = 1;
  709. +
  710. + sec->sh.sh_name = shstrtab->len;
  711. +
  712. + shstrtab->len += strlen(name) + 1;
  713. + shstrtab->changed = true;
  714. +
  715. + return sec;
  716. +}
  717. +
  718. +struct section *elf_create_rela_section(struct elf *elf, struct section *base)
  719. +{
  720. + char *relaname;
  721. + struct section *sec;
  722. +
  723. + relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
  724. + if (!relaname) {
  725. + perror("malloc");
  726. + return NULL;
  727. + }
  728. + strcpy(relaname, ".rela");
  729. + strcat(relaname, base->name);
  730. +
  731. + sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
  732. + if (!sec)
  733. + return NULL;
  734. +
  735. + base->rela = sec;
  736. + sec->base = base;
  737. +
  738. + sec->sh.sh_type = SHT_RELA;
  739. + sec->sh.sh_addralign = 8;
  740. + sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
  741. + sec->sh.sh_info = base->idx;
  742. + sec->sh.sh_flags = SHF_INFO_LINK;
  743. +
  744. + return sec;
  745. +}
  746. +
  747. +int elf_rebuild_rela_section(struct section *sec)
  748. +{
  749. + struct rela *rela;
  750. + int nr, idx = 0, size;
  751. + GElf_Rela *relas;
  752. +
  753. + nr = 0;
  754. + list_for_each_entry(rela, &sec->rela_list, list)
  755. + nr++;
  756. +
  757. + size = nr * sizeof(*relas);
  758. + relas = malloc(size);
  759. + if (!relas) {
  760. + perror("malloc");
  761. + return -1;
  762. + }
  763. +
  764. + sec->data->d_buf = relas;
  765. + sec->data->d_size = size;
  766. +
  767. + sec->sh.sh_size = size;
  768. +
  769. + idx = 0;
  770. + list_for_each_entry(rela, &sec->rela_list, list) {
  771. + relas[idx].r_offset = rela->offset;
  772. + relas[idx].r_addend = rela->addend;
  773. + relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
  774. + idx++;
  775. + }
  776. +
  777. + return 0;
  778. +}
  779. +
  780. +int elf_write(struct elf *elf)
  781. +{
  782. + struct section *sec;
  783. + Elf_Scn *s;
  784. +
  785. + list_for_each_entry(sec, &elf->sections, list) {
  786. + if (sec->changed) {
  787. + s = elf_getscn(elf->elf, sec->idx);
  788. + if (!s) {
  789. + WARN_ELF("elf_getscn");
  790. + return -1;
  791. + }
  792. + if (!gelf_update_shdr (s, &sec->sh)) {
  793. + WARN_ELF("gelf_update_shdr");
  794. + return -1;
  795. + }
  796. + }
  797. + }
  798. +
  799. + if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
  800. + WARN_ELF("elf_update");
  801. + return -1;
  802. + }
  803. +
  804. + return 0;
  805. +}
  806. +
  807. void elf_close(struct elf *elf)
  808. {
  809. struct section *sec, *tmpsec;
  810. diff --git a/tools/objtool/objtool.c b/tools/objtool/objtool.c
  811. index ecc5b1b5d15d..31e0f9143840 100644
  812. --- a/tools/objtool/objtool.c
  813. +++ b/tools/objtool/objtool.c
  814. @@ -42,10 +42,11 @@ struct cmd_struct {
  815. };
  816. static const char objtool_usage_string[] =
  817. - "objtool [OPTIONS] COMMAND [ARGS]";
  818. + "objtool COMMAND [ARGS]";
  819. static struct cmd_struct objtool_cmds[] = {
  820. {"check", cmd_check, "Perform stack metadata validation on an object file" },
  821. + {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
  822. };
  823. bool help;
  824. diff --git a/tools/objtool/orc_dump.c b/tools/objtool/orc_dump.c
  825. new file mode 100644
  826. index 000000000000..36c5bf6a2675
  827. --- /dev/null
  828. +++ b/tools/objtool/orc_dump.c
  829. @@ -0,0 +1,212 @@
  830. +/*
  831. + * Copyright (C) 2017 Josh Poimboeuf <[email protected]>
  832. + *
  833. + * This program is free software; you can redistribute it and/or
  834. + * modify it under the terms of the GNU General Public License
  835. + * as published by the Free Software Foundation; either version 2
  836. + * of the License, or (at your option) any later version.
  837. + *
  838. + * This program is distributed in the hope that it will be useful,
  839. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  840. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  841. + * GNU General Public License for more details.
  842. + *
  843. + * You should have received a copy of the GNU General Public License
  844. + * along with this program; if not, see <http://www.gnu.org/licenses/>.
  845. + */
  846. +
  847. +#include <unistd.h>
  848. +#include "orc.h"
  849. +#include "warn.h"
  850. +
  851. +static const char *reg_name(unsigned int reg)
  852. +{
  853. + switch (reg) {
  854. + case ORC_REG_PREV_SP:
  855. + return "prevsp";
  856. + case ORC_REG_DX:
  857. + return "dx";
  858. + case ORC_REG_DI:
  859. + return "di";
  860. + case ORC_REG_BP:
  861. + return "bp";
  862. + case ORC_REG_SP:
  863. + return "sp";
  864. + case ORC_REG_R10:
  865. + return "r10";
  866. + case ORC_REG_R13:
  867. + return "r13";
  868. + case ORC_REG_BP_INDIRECT:
  869. + return "bp(ind)";
  870. + case ORC_REG_SP_INDIRECT:
  871. + return "sp(ind)";
  872. + default:
  873. + return "?";
  874. + }
  875. +}
  876. +
  877. +static const char *orc_type_name(unsigned int type)
  878. +{
  879. + switch (type) {
  880. + case ORC_TYPE_CALL:
  881. + return "call";
  882. + case ORC_TYPE_REGS:
  883. + return "regs";
  884. + case ORC_TYPE_REGS_IRET:
  885. + return "iret";
  886. + default:
  887. + return "?";
  888. + }
  889. +}
  890. +
  891. +static void print_reg(unsigned int reg, int offset)
  892. +{
  893. + if (reg == ORC_REG_BP_INDIRECT)
  894. + printf("(bp%+d)", offset);
  895. + else if (reg == ORC_REG_SP_INDIRECT)
  896. + printf("(sp%+d)", offset);
  897. + else if (reg == ORC_REG_UNDEFINED)
  898. + printf("(und)");
  899. + else
  900. + printf("%s%+d", reg_name(reg), offset);
  901. +}
  902. +
  903. +int orc_dump(const char *_objname)
  904. +{
  905. + int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
  906. + struct orc_entry *orc = NULL;
  907. + char *name;
  908. + unsigned long nr_sections, orc_ip_addr = 0;
  909. + size_t shstrtab_idx;
  910. + Elf *elf;
  911. + Elf_Scn *scn;
  912. + GElf_Shdr sh;
  913. + GElf_Rela rela;
  914. + GElf_Sym sym;
  915. + Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
  916. +
  917. +
  918. + objname = _objname;
  919. +
  920. + elf_version(EV_CURRENT);
  921. +
  922. + fd = open(objname, O_RDONLY);
  923. + if (fd == -1) {
  924. + perror("open");
  925. + return -1;
  926. + }
  927. +
  928. + elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  929. + if (!elf) {
  930. + WARN_ELF("elf_begin");
  931. + return -1;
  932. + }
  933. +
  934. + if (elf_getshdrnum(elf, &nr_sections)) {
  935. + WARN_ELF("elf_getshdrnum");
  936. + return -1;
  937. + }
  938. +
  939. + if (elf_getshdrstrndx(elf, &shstrtab_idx)) {
  940. + WARN_ELF("elf_getshdrstrndx");
  941. + return -1;
  942. + }
  943. +
  944. + for (i = 0; i < nr_sections; i++) {
  945. + scn = elf_getscn(elf, i);
  946. + if (!scn) {
  947. + WARN_ELF("elf_getscn");
  948. + return -1;
  949. + }
  950. +
  951. + if (!gelf_getshdr(scn, &sh)) {
  952. + WARN_ELF("gelf_getshdr");
  953. + return -1;
  954. + }
  955. +
  956. + name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
  957. + if (!name) {
  958. + WARN_ELF("elf_strptr");
  959. + return -1;
  960. + }
  961. +
  962. + data = elf_getdata(scn, NULL);
  963. + if (!data) {
  964. + WARN_ELF("elf_getdata");
  965. + return -1;
  966. + }
  967. +
  968. + if (!strcmp(name, ".symtab")) {
  969. + symtab = data;
  970. + } else if (!strcmp(name, ".orc_unwind")) {
  971. + orc = data->d_buf;
  972. + orc_size = sh.sh_size;
  973. + } else if (!strcmp(name, ".orc_unwind_ip")) {
  974. + orc_ip = data->d_buf;
  975. + orc_ip_addr = sh.sh_addr;
  976. + } else if (!strcmp(name, ".rela.orc_unwind_ip")) {
  977. + rela_orc_ip = data;
  978. + }
  979. + }
  980. +
  981. + if (!symtab || !orc || !orc_ip)
  982. + return 0;
  983. +
  984. + if (orc_size % sizeof(*orc) != 0) {
  985. + WARN("bad .orc_unwind section size");
  986. + return -1;
  987. + }
  988. +
  989. + nr_entries = orc_size / sizeof(*orc);
  990. + for (i = 0; i < nr_entries; i++) {
  991. + if (rela_orc_ip) {
  992. + if (!gelf_getrela(rela_orc_ip, i, &rela)) {
  993. + WARN_ELF("gelf_getrela");
  994. + return -1;
  995. + }
  996. +
  997. + if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) {
  998. + WARN_ELF("gelf_getsym");
  999. + return -1;
  1000. + }
  1001. +
  1002. + scn = elf_getscn(elf, sym.st_shndx);
  1003. + if (!scn) {
  1004. + WARN_ELF("elf_getscn");
  1005. + return -1;
  1006. + }
  1007. +
  1008. + if (!gelf_getshdr(scn, &sh)) {
  1009. + WARN_ELF("gelf_getshdr");
  1010. + return -1;
  1011. + }
  1012. +
  1013. + name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
  1014. + if (!name || !*name) {
  1015. + WARN_ELF("elf_strptr");
  1016. + return -1;
  1017. + }
  1018. +
  1019. + printf("%s+%lx:", name, rela.r_addend);
  1020. +
  1021. + } else {
  1022. + printf("%lx:", orc_ip_addr + (i * sizeof(int)) + orc_ip[i]);
  1023. + }
  1024. +
  1025. +
  1026. + printf(" sp:");
  1027. +
  1028. + print_reg(orc[i].sp_reg, orc[i].sp_offset);
  1029. +
  1030. + printf(" bp:");
  1031. +
  1032. + print_reg(orc[i].bp_reg, orc[i].bp_offset);
  1033. +
  1034. + printf(" type:%s\n", orc_type_name(orc[i].type));
  1035. + }
  1036. +
  1037. + elf_end(elf);
  1038. + close(fd);
  1039. +
  1040. + return 0;
  1041. +}
  1042. diff --git a/tools/objtool/orc_gen.c b/tools/objtool/orc_gen.c
  1043. new file mode 100644
  1044. index 000000000000..e5ca31429c9b
  1045. --- /dev/null
  1046. +++ b/tools/objtool/orc_gen.c
  1047. @@ -0,0 +1,214 @@
  1048. +/*
  1049. + * Copyright (C) 2017 Josh Poimboeuf <[email protected]>
  1050. + *
  1051. + * This program is free software; you can redistribute it and/or
  1052. + * modify it under the terms of the GNU General Public License
  1053. + * as published by the Free Software Foundation; either version 2
  1054. + * of the License, or (at your option) any later version.
  1055. + *
  1056. + * This program is distributed in the hope that it will be useful,
  1057. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1058. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1059. + * GNU General Public License for more details.
  1060. + *
  1061. + * You should have received a copy of the GNU General Public License
  1062. + * along with this program; if not, see <http://www.gnu.org/licenses/>.
  1063. + */
  1064. +
  1065. +#include <stdlib.h>
  1066. +#include <string.h>
  1067. +
  1068. +#include "orc.h"
  1069. +#include "check.h"
  1070. +#include "warn.h"
  1071. +
  1072. +int create_orc(struct objtool_file *file)
  1073. +{
  1074. + struct instruction *insn;
  1075. +
  1076. + for_each_insn(file, insn) {
  1077. + struct orc_entry *orc = &insn->orc;
  1078. + struct cfi_reg *cfa = &insn->state.cfa;
  1079. + struct cfi_reg *bp = &insn->state.regs[CFI_BP];
  1080. +
  1081. + if (cfa->base == CFI_UNDEFINED) {
  1082. + orc->sp_reg = ORC_REG_UNDEFINED;
  1083. + continue;
  1084. + }
  1085. +
  1086. + switch (cfa->base) {
  1087. + case CFI_SP:
  1088. + orc->sp_reg = ORC_REG_SP;
  1089. + break;
  1090. + case CFI_SP_INDIRECT:
  1091. + orc->sp_reg = ORC_REG_SP_INDIRECT;
  1092. + break;
  1093. + case CFI_BP:
  1094. + orc->sp_reg = ORC_REG_BP;
  1095. + break;
  1096. + case CFI_BP_INDIRECT:
  1097. + orc->sp_reg = ORC_REG_BP_INDIRECT;
  1098. + break;
  1099. + case CFI_R10:
  1100. + orc->sp_reg = ORC_REG_R10;
  1101. + break;
  1102. + case CFI_R13:
  1103. + orc->sp_reg = ORC_REG_R13;
  1104. + break;
  1105. + case CFI_DI:
  1106. + orc->sp_reg = ORC_REG_DI;
  1107. + break;
  1108. + case CFI_DX:
  1109. + orc->sp_reg = ORC_REG_DX;
  1110. + break;
  1111. + default:
  1112. + WARN_FUNC("unknown CFA base reg %d",
  1113. + insn->sec, insn->offset, cfa->base);
  1114. + return -1;
  1115. + }
  1116. +
  1117. + switch(bp->base) {
  1118. + case CFI_UNDEFINED:
  1119. + orc->bp_reg = ORC_REG_UNDEFINED;
  1120. + break;
  1121. + case CFI_CFA:
  1122. + orc->bp_reg = ORC_REG_PREV_SP;
  1123. + break;
  1124. + case CFI_BP:
  1125. + orc->bp_reg = ORC_REG_BP;
  1126. + break;
  1127. + default:
  1128. + WARN_FUNC("unknown BP base reg %d",
  1129. + insn->sec, insn->offset, bp->base);
  1130. + return -1;
  1131. + }
  1132. +
  1133. + orc->sp_offset = cfa->offset;
  1134. + orc->bp_offset = bp->offset;
  1135. + orc->type = insn->state.type;
  1136. + }
  1137. +
  1138. + return 0;
  1139. +}
  1140. +
  1141. +static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,
  1142. + unsigned int idx, struct section *insn_sec,
  1143. + unsigned long insn_off, struct orc_entry *o)
  1144. +{
  1145. + struct orc_entry *orc;
  1146. + struct rela *rela;
  1147. +
  1148. + /* populate ORC data */
  1149. + orc = (struct orc_entry *)u_sec->data->d_buf + idx;
  1150. + memcpy(orc, o, sizeof(*orc));
  1151. +
  1152. + /* populate rela for ip */
  1153. + rela = malloc(sizeof(*rela));
  1154. + if (!rela) {
  1155. + perror("malloc");
  1156. + return -1;
  1157. + }
  1158. + memset(rela, 0, sizeof(*rela));
  1159. +
  1160. + rela->sym = insn_sec->sym;
  1161. + rela->addend = insn_off;
  1162. + rela->type = R_X86_64_PC32;
  1163. + rela->offset = idx * sizeof(int);
  1164. +
  1165. + list_add_tail(&rela->list, &ip_relasec->rela_list);
  1166. + hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
  1167. +
  1168. + return 0;
  1169. +}
  1170. +
  1171. +int create_orc_sections(struct objtool_file *file)
  1172. +{
  1173. + struct instruction *insn, *prev_insn;
  1174. + struct section *sec, *u_sec, *ip_relasec;
  1175. + unsigned int idx;
  1176. +
  1177. + struct orc_entry empty = {
  1178. + .sp_reg = ORC_REG_UNDEFINED,
  1179. + .bp_reg = ORC_REG_UNDEFINED,
  1180. + .type = ORC_TYPE_CALL,
  1181. + };
  1182. +
  1183. + sec = find_section_by_name(file->elf, ".orc_unwind");
  1184. + if (sec) {
  1185. + WARN("file already has .orc_unwind section, skipping");
  1186. + return -1;
  1187. + }
  1188. +
  1189. + /* count the number of needed orcs */
  1190. + idx = 0;
  1191. + for_each_sec(file, sec) {
  1192. + if (!sec->text)
  1193. + continue;
  1194. +
  1195. + prev_insn = NULL;
  1196. + sec_for_each_insn(file, sec, insn) {
  1197. + if (!prev_insn ||
  1198. + memcmp(&insn->orc, &prev_insn->orc,
  1199. + sizeof(struct orc_entry))) {
  1200. + idx++;
  1201. + }
  1202. + prev_insn = insn;
  1203. + }
  1204. +
  1205. + /* section terminator */
  1206. + if (prev_insn)
  1207. + idx++;
  1208. + }
  1209. + if (!idx)
  1210. + return -1;
  1211. +
  1212. +
  1213. + /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
  1214. + sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), idx);
  1215. +
  1216. + ip_relasec = elf_create_rela_section(file->elf, sec);
  1217. + if (!ip_relasec)
  1218. + return -1;
  1219. +
  1220. + /* create .orc_unwind section */
  1221. + u_sec = elf_create_section(file->elf, ".orc_unwind",
  1222. + sizeof(struct orc_entry), idx);
  1223. +
  1224. + /* populate sections */
  1225. + idx = 0;
  1226. + for_each_sec(file, sec) {
  1227. + if (!sec->text)
  1228. + continue;
  1229. +
  1230. + prev_insn = NULL;
  1231. + sec_for_each_insn(file, sec, insn) {
  1232. + if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
  1233. + sizeof(struct orc_entry))) {
  1234. +
  1235. + if (create_orc_entry(u_sec, ip_relasec, idx,
  1236. + insn->sec, insn->offset,
  1237. + &insn->orc))
  1238. + return -1;
  1239. +
  1240. + idx++;
  1241. + }
  1242. + prev_insn = insn;
  1243. + }
  1244. +
  1245. + /* section terminator */
  1246. + if (prev_insn) {
  1247. + if (create_orc_entry(u_sec, ip_relasec, idx,
  1248. + prev_insn->sec,
  1249. + prev_insn->offset + prev_insn->len,
  1250. + &empty))
  1251. + return -1;
  1252. +
  1253. + idx++;
  1254. + }
  1255. + }
  1256. +
  1257. + if (elf_rebuild_rela_section(ip_relasec))
  1258. + return -1;
  1259. +
  1260. + return 0;
  1261. +}
  1262. diff --git a/tools/objtool/Build b/tools/objtool/Build
  1263. index 6f2e1987c4d9..749becdf5b90 100644
  1264. --- a/tools/objtool/Build
  1265. +++ b/tools/objtool/Build
  1266. @@ -1,6 +1,9 @@
  1267. objtool-y += arch/$(SRCARCH)/
  1268. objtool-y += builtin-check.o
  1269. +objtool-y += builtin-orc.o
  1270. objtool-y += check.o
  1271. +objtool-y += orc_gen.o
  1272. +objtool-y += orc_dump.o
  1273. objtool-y += elf.o
  1274. objtool-y += special.o
  1275. objtool-y += objtool.o
  1276. --
  1277. 2.14.2