Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_ELFNOTE_H
0003 #define _LINUX_ELFNOTE_H
0004 /*
0005  * Helper macros to generate ELF Note structures, which are put into a
0006  * PT_NOTE segment of the final vmlinux image.  These are useful for
0007  * including name-value pairs of metadata into the kernel binary (or
0008  * modules?) for use by external programs.
0009  *
0010  * Each note has three parts: a name, a type and a desc.  The name is
0011  * intended to distinguish the note's originator, so it would be a
0012  * company, project, subsystem, etc; it must be in a suitable form for
0013  * use in a section name.  The type is an integer which is used to tag
0014  * the data, and is considered to be within the "name" namespace (so
0015  * "FooCo"'s type 42 is distinct from "BarProj"'s type 42).  The
0016  * "desc" field is the actual data.  There are no constraints on the
0017  * desc field's contents, though typically they're fairly small.
0018  *
0019  * All notes from a given NAME are put into a section named
0020  * .note.NAME.  When the kernel image is finally linked, all the notes
0021  * are packed into a single .notes section, which is mapped into the
0022  * PT_NOTE segment.  Because notes for a given name are grouped into
0023  * the same section, they'll all be adjacent the output file.
0024  *
0025  * This file defines macros for both C and assembler use.  Their
0026  * syntax is slightly different, but they're semantically similar.
0027  *
0028  * See the ELF specification for more detail about ELF notes.
0029  */
0030 
0031 #ifdef __ASSEMBLER__
0032 /*
0033  * Generate a structure with the same shape as Elf{32,64}_Nhdr (which
0034  * turn out to be the same size and shape), followed by the name and
0035  * desc data with appropriate padding.  The 'desctype' argument is the
0036  * assembler pseudo op defining the type of the data e.g. .asciz while
0037  * 'descdata' is the data itself e.g.  "hello, world".
0038  *
0039  * e.g. ELFNOTE(XYZCo, 42, .asciz, "forty-two")
0040  *      ELFNOTE(XYZCo, 12, .long, 0xdeadbeef)
0041  */
0042 #define ELFNOTE_START(name, type, flags)    \
0043 .pushsection .note.name, flags,@note    ;   \
0044   .balign 4             ;   \
0045   .long 2f - 1f     /* namesz */    ;   \
0046   .long 4484f - 3f  /* descsz */    ;   \
0047   .long type                ;   \
0048 1:.asciz #name              ;   \
0049 2:.balign 4             ;   \
0050 3:
0051 
0052 #define ELFNOTE_END             \
0053 4484:.balign 4              ;   \
0054 .popsection             ;
0055 
0056 #define ELFNOTE(name, type, desc)       \
0057     ELFNOTE_START(name, type, "a")      \
0058         desc            ;   \
0059     ELFNOTE_END
0060 
0061 #else   /* !__ASSEMBLER__ */
0062 #include <uapi/linux/elf.h>
0063 /*
0064  * Use an anonymous structure which matches the shape of
0065  * Elf{32,64}_Nhdr, but includes the name and desc data.  The size and
0066  * type of name and desc depend on the macro arguments.  "name" must
0067  * be a literal string, and "desc" must be passed by value.  You may
0068  * only define one note per line, since __LINE__ is used to generate
0069  * unique symbols.
0070  */
0071 #define _ELFNOTE_PASTE(a,b) a##b
0072 #define _ELFNOTE(size, name, unique, type, desc)            \
0073     static const struct {                       \
0074         struct elf##size##_note _nhdr;              \
0075         unsigned char _name[sizeof(name)]           \
0076         __attribute__((aligned(sizeof(Elf##size##_Word)))); \
0077         typeof(desc) _desc                  \
0078                  __attribute__((aligned(sizeof(Elf##size##_Word)))); \
0079     } _ELFNOTE_PASTE(_note_, unique)                \
0080         __used                          \
0081         __attribute__((section(".note." name),          \
0082                    aligned(sizeof(Elf##size##_Word)),   \
0083                    unused)) = {             \
0084         {                           \
0085             sizeof(name),                   \
0086             sizeof(desc),                   \
0087             type,                       \
0088         },                          \
0089         name,                           \
0090         desc                            \
0091     }
0092 #define ELFNOTE(size, name, type, desc)     \
0093     _ELFNOTE(size, name, __LINE__, type, desc)
0094 
0095 #define ELFNOTE32(name, type, desc) ELFNOTE(32, name, type, desc)
0096 #define ELFNOTE64(name, type, desc) ELFNOTE(64, name, type, desc)
0097 #endif  /* __ASSEMBLER__ */
0098 
0099 #endif /* _LINUX_ELFNOTE_H */