On Wed, Dec 11, 2024 at 8:40 AM Alastair Robertson <ajor@xxxxxxxx> wrote: > > Move the filename arguments and file-descriptor handling from > init_output_elf() and linker_load_obj_file() and instead handle them > at the top-level in bpf_linker__new() and bpf_linker__add_file(). > > This will allow the inner functions to be shared with a new, > non-filename-based, API in the next commit. > > Signed-off-by: Alastair Robertson <ajor@xxxxxxxx> > --- > tools/lib/bpf/linker.c | 83 +++++++++++++++++++++--------------------- > 1 file changed, 41 insertions(+), 42 deletions(-) > [...] > @@ -440,7 +439,7 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, > const struct bpf_linker_file_opts *opts) > { > struct src_obj obj = {}; > - int err = 0; > + int err = 0, fd; > > if (!OPTS_VALID(opts, bpf_linker_file_opts)) > return libbpf_err(-EINVAL); > @@ -448,7 +447,16 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, > if (!linker->elf) > return libbpf_err(-EINVAL); > > - err = err ?: linker_load_obj_file(linker, filename, opts, &obj); > + fd = open(filename, O_RDONLY | O_CLOEXEC); > + if (fd < 0) { > + pr_warn("failed to open file '%s': %s\n", filename, errstr(errno)); > + return -errno; errno can be clobbered by pr_warn(), so need to save it like in another pr_warn case above, fixed while applying > + } > + > + obj.filename = filename; > + obj.fd = fd; > + > + err = err ?: linker_load_obj_file(linker, &obj); > err = err ?: linker_append_sec_data(linker, &obj); > err = err ?: linker_append_elf_syms(linker, &obj); > err = err ?: linker_append_elf_relos(linker, &obj); > @@ -534,8 +542,7 @@ static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name) > return sec; > } > > -static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, > - const struct bpf_linker_file_opts *opts, > +static int linker_load_obj_file(struct bpf_linker *linker, > struct src_obj *obj) > { > int err = 0; [...]