elfnote.h 3.5 KB

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