elfnote.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_ELFNOTE_H
  3. #define _LINUX_ELFNOTE_H
  4. /*
  5. * Helper macros to generate ELF Note structures, which are put into a
  6. * PT_NOTE segment of the final vmlinux image. These are useful for
  7. * including name-value pairs of metadata into the kernel binary (or
  8. * modules?) for use by external programs.
  9. *
  10. * Each note has three parts: a name, a type and a desc. The name is
  11. * intended to distinguish the note's originator, so it would be a
  12. * company, project, subsystem, etc; it must be in a suitable form for
  13. * use in a section name. The type is an integer which is used to tag
  14. * the data, and is considered to be within the "name" namespace (so
  15. * "FooCo"'s type 42 is distinct from "BarProj"'s type 42). The
  16. * "desc" field is the actual data. There are no constraints on the
  17. * desc field's contents, though typically they're fairly small.
  18. *
  19. * All notes from a given NAME are put into a section named
  20. * .note.NAME. When the kernel image is finally linked, all the notes
  21. * are packed into a single .notes section, which is mapped into the
  22. * PT_NOTE segment. Because notes for a given name are grouped into
  23. * the same section, they'll all be adjacent the output file.
  24. *
  25. * This file defines macros for both C and assembler use. Their
  26. * syntax is slightly different, but they're semantically similar.
  27. *
  28. * See the ELF specification for more detail about ELF notes.
  29. */
  30. #ifdef __ASSEMBLER__
  31. /*
  32. * Generate a structure with the same shape as Elf{32,64}_Nhdr (which
  33. * turn out to be the same size and shape), followed by the name and
  34. * desc data with appropriate padding. The 'desctype' argument is the
  35. * assembler pseudo op defining the type of the data e.g. .asciz while
  36. * 'descdata' is the data itself e.g. "hello, world".
  37. *
  38. * e.g. ELFNOTE(XYZCo, 42, .asciz, "forty-two")
  39. * ELFNOTE(XYZCo, 12, .long, 0xdeadbeef)
  40. */
  41. #define ELFNOTE_START(name, type, flags) \
  42. .pushsection .note.name, flags,@note ; \
  43. .balign 4 ; \
  44. .long 2f - 1f /* namesz */ ; \
  45. .long 4484f - 3f /* descsz */ ; \
  46. .long type ; \
  47. 1:.asciz #name ; \
  48. 2:.balign 4 ; \
  49. 3:
  50. #define ELFNOTE_END \
  51. 4484:.balign 4 ; \
  52. .popsection ;
  53. #define ELFNOTE(name, type, desc) \
  54. ELFNOTE_START(name, type, "a") \
  55. desc ; \
  56. ELFNOTE_END
  57. #else /* !__ASSEMBLER__ */
  58. #include <uapi/linux/elf.h>
  59. /*
  60. * Use an anonymous structure which matches the shape of
  61. * Elf{32,64}_Nhdr, but includes the name and desc data. The size and
  62. * type of name and desc depend on the macro arguments. "name" must
  63. * be a literal string, and "desc" must be passed by value. You may
  64. * only define one note per line, since __LINE__ is used to generate
  65. * unique symbols.
  66. */
  67. #define _ELFNOTE_PASTE(a,b) a##b
  68. #define _ELFNOTE(size, name, unique, type, desc) \
  69. static const struct { \
  70. struct elf##size##_note _nhdr; \
  71. unsigned char _name[sizeof(name)] \
  72. __attribute__((aligned(sizeof(Elf##size##_Word)))); \
  73. typeof(desc) _desc \
  74. __attribute__((aligned(sizeof(Elf##size##_Word)))); \
  75. } _ELFNOTE_PASTE(_note_, unique) \
  76. __used \
  77. __attribute__((section(".note." name), \
  78. aligned(sizeof(Elf##size##_Word)), \
  79. unused)) = { \
  80. { \
  81. sizeof(name), \
  82. sizeof(desc), \
  83. type, \
  84. }, \
  85. name, \
  86. desc \
  87. }
  88. #define ELFNOTE(size, name, type, desc) \
  89. _ELFNOTE(size, name, __LINE__, type, desc)
  90. #define ELFNOTE32(name, type, desc) ELFNOTE(32, name, type, desc)
  91. #define ELFNOTE64(name, type, desc) ELFNOTE(64, name, type, desc)
  92. #endif /* __ASSEMBLER__ */
  93. #endif /* _LINUX_ELFNOTE_H */