pahole: Save header guard names when --header_guards_db is passed
Eduard Zingerman
maintainercover pahole: Save header guard names when --header_guards_db is passed
This is a supporting change for a kernel RFC that should allow using uapi headers with vmlinux.h. For this it is necessary to add #ifndef GUARD ... #ifdef brackets around some types if those types originate from uapi kernel headers.
For example:
include/uapi/linux/tcp.h:
#ifndef _UAPI_LINUX_TCP_H
#define _UAPI_LINUX_TCP_H
...
union tcp_word_hdr {
struct tcphdr hdr;
__be32 words[5]; };
...
#endif /* _UAPI_LINUX_TCP_H */
vmlinux.h:
... #ifndef _UAPI_LINUX_TCP_H
union tcp_word_hdr {
struct tcphdr hdr;
__be32 words[5];};
#endif ...
The information about the GUARD names has to be encoded in BTF to allow bpftool to generate it for a specific kernel binary.
This is achieved by adding a parameter --header_guards_db to pahole. This parameter should point to a file that associates header file names with names of a C pre-processor variables used as a double include guards (dubbed as "header guard"s).
For each emitted type the DWARF attribute DW_AT_decl_file is checked, when the file name is present in the header guards DB a fake BTF_KIND_DECL_TAG record of the following form is emitted:
- type: the id of the emitted type;
- name_off: a string "header_guard:<guard>".
I'd like to cover this thing with a few unit tests but I don't see any tests in the dwarves repository ([1]), could you please suggest the right location?
I'm going to use a lore link to this thread in the kernel RFC email. I will post a link to the kernel RFC as a reply in this thread as soon as I have one. The change is not useful on it's own, so if kernel RFC will not be accepted this one should not be accepted either.
Thanks,
Eduard
[1] https://github.com/acmel/dwarves
Eduard Zingerman (1):
pahole: Save header guard names when --header_guards_db is passed
btf_encoder.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++---- btf_encoder.h | 3 +- dutil.c | 20 +++++++--- dutil.h | 1 + dwarves.h | 1 + pahole.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 210 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1
Eduard Zingerman
maintainerRFC 1/1 pahole: Save header guard names when --header_guards_db is passed
Adds a parameter --header_guards_db. The parameter should point to a file that associates header file names with names of a C pre-processor variables used as a double include guards (dubbed as "header guard"s).
For each emitted type the DWARF attribute DW_AT_decl_file is checked, when the file name is present in the header guards DB a fake BTF_KIND_DECL_TAG record of the following form is emitted:
- type: the id of the emitted type;
- name_off: a string "header_guard:<guard>".
For example if DB record is present for 'tcphdr' the emitted BTF would look as follows:
$ bpftool btf dump file vmlinux ... [24139] STRUCT 'tcphdr' size=20 vlen=17 ... [24296] DECL_TAG 'header_guard:_UAPI_LINUX_TCP_H' type_id=24139 ...
The DB file format:
- one record per line;
- record format: <file-name> <guard>;
- spaces are not allowed in neither <file-name> nor <guard>.
--- btf_encoder.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++---- btf_encoder.h | 3 +- dutil.c | 20 +++++++--- dutil.h | 1 + dwarves.h | 1 + pahole.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 210 insertions(+), 17 deletions(-)
btf_encoder.c +95 -8
@@ -22,6 +22,7 @@#include <inttypes.h> #include <limits.h> +#include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
@@ -75,6 +76,7 @@ struct btf_encoder {int allocated;int cnt; } functions; +struct strlist *header_guards_db; };void btf_encoders__add (struct list_head *encoders,struct btf_encoder *encoder)
@@ -1408,7 +1410,8 @@ out:return err; } -struct btf_encoder *btf_encoder__new (struct cu *cu,const char *detached_filename,struct btf *base_btf,bool skip_encoding_vars,bool force,bool gen_floats,bool verbose) +struct btf_encoder *btf_encoder__new (struct cu *cu,const char *detached_filename,struct btf *base_btf,bool skip_encoding_vars,bool force,bool gen_floats,bool verbose, +struct strlist *header_guards_db) {struct btf_encoder *encoder =zalloc (sizeof (*encoder));
@@ -1429,6 +1432,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam encoder->has_index_type = false; encoder->need_index_type = false; encoder->array_index_id =0 ; + encoder->header_guards_db = header_guards_db;GElf_Ehdr ehdr;
@@ -1505,6 +1509,68 @@ void btf_encoder__delete(struct btf_encoder *encoder)free (encoder); } +#define DECL_FILE_HEADER_GUARD_TAG "header_guard:" + +/* +* For a given tag check if file name associated with it is present in +* header_guards_db. If present, emit a fake BTF_KIND_DECL_TAG record +* associated with this tag with a value of form "header_guard:<guard-name>". +*/ +int btf_encoder__maybe_add_header_guard_tag (struct btf_encoder *encoder, +struct tag *tag, +struct cu *cu, +int btf_type_id) +{ +const char *decl_file; +char *guard =NULL ; +size_t guard_len; +char buf[256 ]; + +if (tag__type (tag)->declaration) +return 0 ; + + decl_file =tag__decl_file (tag, cu); + +if (!decl_file) +return 0 ; + +if (strstarts (decl_file,"./" )) + decl_file = &decl_file[2 ]; + +/* Ignore a possibility of an absolute path in the file name for now */ + +__strlist__has_entry (encoder->header_guards_db, decl_file, (void **)&guard); +if (!guard) +return 0 ; + + guard_len =strlen (decl_file); +if (guard_len +strlen (DECL_FILE_HEADER_GUARD_TAG ) +1 >sizeof (buf)) { +fprintf (stderr,"error: uapi decl file name '%s' is too long (%lu)\n" , + decl_file, guard_len); +return -1 ; + } +strcpy (buf,DECL_FILE_HEADER_GUARD_TAG ); +strcpy (&buf[sizeof (DECL_FILE_HEADER_GUARD_TAG ) -1 ], guard); + +return btf_encoder__add_decl_tag (encoder, buf, btf_type_id,-1 ); +} + +static const char *dw_tag_printable_name (uint16_t tag) +{ +switch (tag) { +case DW_TAG_structure_type: +return "struct" ; +case DW_TAG_union_type: +return "union" ; +case DW_TAG_typedef: +return "typedef" ; +case DW_TAG_enumeration_type: +return "enum" ; +default : +return "<no_tag_name>" ; + } +} +int btf_encoder__encode_cu (struct btf_encoder *encoder,struct cu *cu,struct conf_load *conf_load) {uint32_t type_id_off =btf__type_cnt (encoder->btf) -1 ;
@@ -1556,17 +1622,11 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct cocu__for_each_type (cu, core_id, pos) {struct namespace *ns; -const char *tag_name;switch (pos->tag) {case DW_TAG_structure_type: - tag_name ="struct" ; -break ;case DW_TAG_union_type: - tag_name ="union" ; -break ;case DW_TAG_typedef: - tag_name ="typedef" ;break ;default :continue ;
@@ -1578,12 +1638,39 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co tag_type_id =btf_encoder__add_decl_tag (encoder, annot->value, btf_type_id, annot->component_idx);if (tag_type_id <0 ) {fprintf (stderr,"error: failed to encode tag '%s' to %s '%s' with component_idx %d\n" , - annot->value, tag_name,namespace__name (ns), annot->component_idx); + annot->value,dw_tag_printable_name (pos->tag), +namespace__name (ns), annot->component_idx); goto out; } } } +cu__for_each_type (cu, core_id, pos) { +struct namespace *ns; + +switch (pos->tag) { +case DW_TAG_structure_type: +case DW_TAG_union_type: +case DW_TAG_typedef: +case DW_TAG_enumeration_type: +break ; +default : +continue ; + } + + btf_type_id = type_id_off + core_id; + tag_type_id =btf_encoder__maybe_add_header_guard_tag (encoder, pos, + cu, btf_type_id); +if (tag_type_id <0 ) { + ns =tag__namespace (pos); +fprintf (stderr, +"error: failed to encode uapi header info for %s tag '%s'\n" , +dw_tag_printable_name (pos->tag), +namespace__name (ns)); + goto out; + } + } +cu__for_each_function (cu, core_id, fn) {int btf_fnproto_id, btf_fn_id;const char *name;
btf_encoder.h +2 -1
@@ -16,7 +16,8 @@ struct btf;struct cu ;struct list_head ; -struct btf_encoder *btf_encoder__new (struct cu *cu,const char *detached_filename,struct btf *base_btf,bool skip_encoding_vars,bool force,bool gen_floats,bool verbose); +struct btf_encoder *btf_encoder__new (struct cu *cu,const char *detached_filename,struct btf *base_btf,bool skip_encoding_vars,bool force,bool gen_floats,bool verbose, +struct strlist *header_guards_db);void btf_encoder__delete (struct btf_encoder *encoder);int btf_encoder__encode (struct btf_encoder *encoder);
dutil.c +14 -6
@@ -156,7 +156,7 @@ void strlist__remove(struct strlist *slist, struct str_node *sn)str_node__delete (sn, slist->dupstr); } -bool strlist__has_entry (struct strlist *slist,const char *entry) +bool __strlist__has_entry (struct strlist *slist,const char *entry,void **priv) {struct rb_node **p = &slist->entries.rb_node;struct rb_node *parent =NULL ;
@@ -169,17 +169,25 @@ bool strlist__has_entry(struct strlist *slist, const char *entry) sn = rb_entry(parent, struct str_node, rb_node); rc =strcmp (sn->s, entry); -if (rc >0 ) - p = &(*p)->rb_left; -else if (rc <0 ) - p = &(*p)->rb_right; -else +if (rc >0 ) { + p = &(*p)->rb_left; + }else if (rc <0 ) { + p = &(*p)->rb_right; + }else { +if (priv) + *priv = sn->priv;return true; + } }return false; } +bool strlist__has_entry (struct strlist *slist,const char *entry) +{ +return __strlist__has_entry (slist, entry,NULL ); +} +Elf_Scn *elf_section_by_name (Elf *elf,GElf_Shdr *shp,const char *name,size_t *index) {Elf_Scn *sec =NULL ;
dutil.h +1 -0
@@ -299,6 +299,7 @@ int strlist__add(struct strlist *slist, const char *str);int __strlist__add (struct strlist *slist,const char *str,void *priv);bool strlist__has_entry (struct strlist *slist,const char *entry); +bool __strlist__has_entry (struct strlist *slist,const char *entry,void **priv);static inline bool strlist__empty (const struct strlist *slist) {
dwarves.h +1 -0
@@ -70,6 +70,7 @@ struct conf_load {uint8_t max_hashtable_bits;uint16_t kabi_prefix_len;const char *kabi_prefix; +const char *header_guards_db_file;struct btf *base_btf;struct conf_fprintf *conf_fprintf;int (*threads_prepare)(struct conf_load *conf,int nr_threads,void **thr_data);
pahole.c +97 -2
153 lines not on this page · raw message
Signature · 1 line
2.34.1