forked from mirrors/linux
		
	bpftool: Provide a helper method for accessing skeleton's embedded ELF data
This adds a skeleton method X__elf_bytes() which returns the binary data of the compiled and embedded BPF object file. 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@fb.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20210901194439.3853238-3-alastorze@fb.com
This commit is contained in:
		
							parent
							
								
									08a6f22ef6
								
							
						
					
					
						commit
						a6cc6b34b9
					
				
					 1 changed files with 19 additions and 12 deletions
				
			
		| 
						 | 
					@ -238,8 +238,8 @@ static void codegen(const char *template, ...)
 | 
				
			||||||
		} else if (c == '\n') {
 | 
							} else if (c == '\n') {
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			p_err("unrecognized character at pos %td in template '%s'",
 | 
								p_err("unrecognized character at pos %td in template '%s': '%c'",
 | 
				
			||||||
			      src - template - 1, template);
 | 
								      src - template - 1, template, c);
 | 
				
			||||||
			free(s);
 | 
								free(s);
 | 
				
			||||||
			exit(-1);
 | 
								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) {
 | 
						bpf_object__for_each_map(map, obj) {
 | 
				
			||||||
		const char * ident;
 | 
							const char *ident;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		ident = get_map_ident(map);
 | 
							ident = get_map_ident(map);
 | 
				
			||||||
		if (!ident)
 | 
							if (!ident)
 | 
				
			||||||
| 
						 | 
					@ -862,6 +862,8 @@ static int do_skeleton(int argc, char **argv)
 | 
				
			||||||
	codegen("\
 | 
						codegen("\
 | 
				
			||||||
		\n\
 | 
							\n\
 | 
				
			||||||
									    \n\
 | 
														    \n\
 | 
				
			||||||
 | 
							static inline const void *%1$s__elf_bytes(size_t *sz);	    \n\
 | 
				
			||||||
 | 
														    \n\
 | 
				
			||||||
		static inline int					    \n\
 | 
							static inline int					    \n\
 | 
				
			||||||
		%1$s__create_skeleton(struct %1$s *obj)			    \n\
 | 
							%1$s__create_skeleton(struct %1$s *obj)			    \n\
 | 
				
			||||||
		{							    \n\
 | 
							{							    \n\
 | 
				
			||||||
| 
						 | 
					@ -943,10 +945,20 @@ static int do_skeleton(int argc, char **argv)
 | 
				
			||||||
	codegen("\
 | 
						codegen("\
 | 
				
			||||||
		\n\
 | 
							\n\
 | 
				
			||||||
									    \n\
 | 
														    \n\
 | 
				
			||||||
			s->data_sz = %d;				    \n\
 | 
								s->data = (void *)%2$s__elf_bytes(&s->data_sz);	    \n\
 | 
				
			||||||
			s->data = (void *)\"\\				    \n\
 | 
														    \n\
 | 
				
			||||||
		",
 | 
								return 0;					    \n\
 | 
				
			||||||
		file_sz);
 | 
							err:							    \n\
 | 
				
			||||||
 | 
								bpf_object__destroy_skeleton(s);		    \n\
 | 
				
			||||||
 | 
								return -ENOMEM;					    \n\
 | 
				
			||||||
 | 
							}							    \n\
 | 
				
			||||||
 | 
														    \n\
 | 
				
			||||||
 | 
							static inline const void *%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 */
 | 
						/* embed contents of BPF object file */
 | 
				
			||||||
	print_hex(obj_data, file_sz);
 | 
						print_hex(obj_data, file_sz);
 | 
				
			||||||
| 
						 | 
					@ -954,11 +966,6 @@ static int do_skeleton(int argc, char **argv)
 | 
				
			||||||
	codegen("\
 | 
						codegen("\
 | 
				
			||||||
		\n\
 | 
							\n\
 | 
				
			||||||
		\";							    \n\
 | 
							\";							    \n\
 | 
				
			||||||
									    \n\
 | 
					 | 
				
			||||||
			return 0;					    \n\
 | 
					 | 
				
			||||||
		err:							    \n\
 | 
					 | 
				
			||||||
			bpf_object__destroy_skeleton(s);		    \n\
 | 
					 | 
				
			||||||
			return -ENOMEM;					    \n\
 | 
					 | 
				
			||||||
		}							    \n\
 | 
							}							    \n\
 | 
				
			||||||
									    \n\
 | 
														    \n\
 | 
				
			||||||
		#endif /* %s */						    \n\
 | 
							#endif /* %s */						    \n\
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue