Skip to content

Commit

Permalink
Add static library support
Browse files Browse the repository at this point in the history
hold can now open archives (smartly!) and link against them.
Also fix a few bugs found in the process.

Signed-off-by: Pedro Falcato <[email protected]>
  • Loading branch information
heatd committed Nov 22, 2023
1 parent 4f07b32 commit 7882cf6
Show file tree
Hide file tree
Showing 9 changed files with 634 additions and 101 deletions.
32 changes: 32 additions & 0 deletions include/elf/ar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* Copyright (C) 2023 Pedro Falcato */
#ifndef ELF_AR_H
#define ELF_AR_H

#include <string.h>

#define AR_MAGIC "!<arch>\n"
#define ARFMAG "`\n"

struct ar_hdr {
char ar_name[16];
char ar_date[12];
char ar_uid[6];
char ar_gid[6];
char ar_mode[8];
char ar_size[10];
char ar_fmag[2];
};

static inline int is_ar_archive(const void *buf)
{
return !memcmp(buf, AR_MAGIC, strlen(AR_MAGIC));
}

struct input_file;
struct symbol;

int parse_ar(struct input_file *file, const void *mapping, unsigned long filesz);
int resolve_lazy(struct symbol *sym);

#endif
44 changes: 41 additions & 3 deletions include/elf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ struct symbol {
* has not been an overridden symbol.
*/
u8 nofree_name : 1;
struct input_section *section;

/* For lazy symbols, this holds the position in the ar archive */
u32 ar_pos;
union {
struct input_section *section;
struct input_file *file;
};

struct symbol *next;
};
Expand Down Expand Up @@ -81,16 +87,40 @@ struct input_section {
muptr output_off;
};

struct ar_archive_data;

struct input_file_ops {
int (*open)(struct input_file *file);
void (*read)(struct input_file *file, void *buf, uptr size, uptr offset);
void (*close)(struct input_file *file);
};

struct input_file {
const char *name;
u16 emachine;
u32 free_name : 1;

struct input_section *sections;
u32 nsections;
struct symbol *syms;
u32 nsyms;
struct relocation *relocs;
u32 nrelocs;

struct ar_archive_data *ardata;

struct {
u8 *long_file_names;
} archive;

union {
int fd;
struct {
uptr archive_off;
};
} openf;

const struct input_file_ops *ops;
};

typedef Elf64_Ehdr elf_ehdr;
Expand All @@ -102,13 +132,13 @@ typedef Elf64_Phdr elf_phdr;
int process_rela(struct input_file *file, elf_shdr *section, u8 *mapping);
struct symbol *lookup_symtable(const char *name);

struct output_section *elf_merge_sections(struct input_file **files, u32 nfiles,
struct output_section **elf_merge_sections(struct input_file **files, u32 nfiles,
u32 *p_noutput);

struct program_header;

struct elf_writer {
struct output_section *out_section;
struct output_section **out_section;
u32 nr_output_secs;
struct hold_options *options;
struct input_file **files;
Expand All @@ -126,4 +156,12 @@ void relocate_symbols(void);

void elf_do_relocs(struct input_file *file, struct relocation *relocs, u32 nrelocs, u8 *mapping);

int add_to_symtable(struct symbol *sym);

/* If set, we should free this filename (as it was malloc'd) */
#define ELF_PROCESS_FILENAME_FREE (1 << 0)

int elf_process_objfile(const char *filename, void *map, uptr fd_size,
const struct input_file_ops *ops, int flags);

#endif
Loading

0 comments on commit 7882cf6

Please sign in to comment.