[PATCH v2 bpf-next 2/3] bpftool: Provide a helper method for accessing bpf binary data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This adds a binary method X_elf_bytes() which returns the binary data of
the compiled bpf object.  It additionally sets the size of the return
data to the provided size_t pointer argument.

The assignment to s->data is cast to void * to ensure no warning is
issued if compiled with a previous version of libbpf where the
bpf_object_skeleton field is void * instead of const void *

Signed-off-by: Matt Smith <alastorze@xxxxxx>
---
 tools/bpf/bpftool/gen.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index d40d92bbf0e4..f54dc4792fce 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -238,8 +238,8 @@ static void codegen(const char *template, ...)
 		} else if (c == '\n') {
 			break;
 		} else {
-			p_err("unrecognized character at pos %td in template '%s'",
-			      src - template - 1, template);
+			p_err("unrecognized character at pos %td in template '%s': '%c'",
+			      src - template - 1, template, c);
 			free(s);
 			exit(-1);
 		}
@@ -406,7 +406,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
 	}
 
 	bpf_object__for_each_map(map, obj) {
-		const char * ident;
+		const char *ident;
 
 		ident = get_map_ident(map);
 		if (!ident)
@@ -787,6 +787,9 @@ static int do_skeleton(int argc, char **argv)
 			free(obj);					    \n\
 		}							    \n\
 									    \n\
+		static inline const void *		\n\
+		%1$s__elf_bytes(size_t *sz);	\n\
+										\n\
 		static inline int					    \n\
 		%1$s__create_skeleton(struct %1$s *obj);		    \n\
 									    \n\
@@ -943,25 +946,31 @@ static int do_skeleton(int argc, char **argv)
 	codegen("\
 		\n\
 									    \n\
-			s->data_sz = %d;				    \n\
-			s->data = (void *)\"\\				    \n\
-		",
-		file_sz);
-
-	/* embed contents of BPF object file */
-	print_hex(obj_data, file_sz);
-
-	codegen("\
+			s->data = (void *)%2$s__elf_bytes(&s->data_sz);			    \n\
 		\n\
-		\";							    \n\
 									    \n\
 			return 0;					    \n\
 		err:							    \n\
 			bpf_object__destroy_skeleton(s);		    \n\
 			return -ENOMEM;					    \n\
 		}							    \n\
-									    \n\
-		#endif /* %s */						    \n\
+										\n\
+		static inline const void *		\n\
+		%2$s__elf_bytes(size_t *sz)		\n\
+		{								\n\
+			*sz = %1$d;					\n\
+			return (const void *)\"\\	\n\
+		"
+		, file_sz, obj_name);
+
+	/* embed contents of BPF object file */
+	print_hex(obj_data, file_sz);
+
+	codegen("\
+		\n\
+		\";								\n\
+		}								\n\
+		#endif /* %s */					\n\
 		",
 		header_guard);
 	err = 0;
-- 
2.30.2





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux