FreeBSD kernel kern code
link_elf.c File Reference
#include <sys/cdefs.h>
#include "opt_ddb.h"
#include "opt_gdb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/mount.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/namei.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/linker.h>
#include <sys/sysctl.h>
#include <machine/elf.h>
#include <net/vnet.h>
#include <security/mac/mac_framework.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <sys/link_elf.h>
#include "linker_if.h"
#include <kern/kern_ctf.c>
Include dependency graph for link_elf.c:

Go to the source code of this file.

Data Structures

struct  elf_file
 
struct  elf_set
 

Macros

#define MAXSEGS   4
 
#define LS_PADDING   0x90909090
 
#define APPLY_RELOCS(iter, tbl, tblsize, type)
 

Typedefs

typedef struct elf_fileelf_file_t
 
typedef int(* elf_reloc_fn) (linker_file_t lf, Elf_Addr relocbase, const void *data, int type, elf_lookup_fn lookup)
 

Functions

 __FBSDID ("$FreeBSD$")
 
 TAILQ_HEAD (elf_set_head, elf_set)
 
static int link_elf_link_common_finish (linker_file_t)
 
static int link_elf_link_preload (linker_class_t cls, const char *, linker_file_t *)
 
static int link_elf_link_preload_finish (linker_file_t)
 
static int link_elf_load_file (linker_class_t, const char *, linker_file_t *)
 
static int link_elf_lookup_symbol (linker_file_t, const char *, c_linker_sym_t *)
 
static int link_elf_lookup_debug_symbol (linker_file_t, const char *, c_linker_sym_t *)
 
static int link_elf_symbol_values (linker_file_t, c_linker_sym_t, linker_symval_t *)
 
static int link_elf_debug_symbol_values (linker_file_t, c_linker_sym_t, linker_symval_t *)
 
static int link_elf_search_symbol (linker_file_t, caddr_t, c_linker_sym_t *, long *)
 
static void link_elf_unload_file (linker_file_t)
 
static void link_elf_unload_preload (linker_file_t)
 
static int link_elf_lookup_set (linker_file_t, const char *, void ***, void ***, int *)
 
static int link_elf_each_function_name (linker_file_t, int(*)(const char *, void *), void *)
 
static int link_elf_each_function_nameval (linker_file_t, linker_function_nameval_callback_t, void *)
 
static void link_elf_reloc_local (linker_file_t)
 
static long link_elf_symtab_get (linker_file_t, const Elf_Sym **)
 
static long link_elf_strtab_get (linker_file_t, caddr_t *)
 
static int elf_lookup (linker_file_t, Elf_Size, int, Elf_Addr *)
 
 SYSCTL_BOOL (_debug, OID_AUTO, link_elf_leak_locals, CTLFLAG_RWTUN, &link_elf_leak_locals, 0, "Allow local symbols to participate in global module symbol resolution")
 
static int parse_dynamic (elf_file_t)
 
static int relocate_file (elf_file_t)
 
static int relocate_file1 (elf_file_t ef, elf_lookup_fn lookup, elf_reloc_fn reloc, bool ifuncs)
 
static int link_elf_preload_parse_symbols (elf_file_t)
 
static void elf_set_add (struct elf_set_head *list, Elf_Addr start, Elf_Addr stop, Elf_Addr base)
 
static int elf_set_find (struct elf_set_head *list, Elf_Addr addr, Elf_Addr *start, Elf_Addr *base)
 
static void elf_set_delete (struct elf_set_head *list, Elf_Addr start)
 
static void link_elf_error (const char *filename, const char *s)
 
static void link_elf_invoke_ctors (caddr_t addr, size_t size)
 
 SYSCTL_ULONG (_kern, OID_AUTO, base_address, CTLFLAG_RD, SYSCTL_NULL_ULONG_PTR, KERNBASE, "Kernel base address")
 
 SYSCTL_ULONG (_kern, OID_AUTO, relbase_address, CTLFLAG_RD, &kern_relbase, 0, "Kernel relocated base address")
 
static void link_elf_init (void *arg)
 
 SYSINIT (link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, NULL)
 
static int parse_dpcpu (elf_file_t ef)
 
static int preload_protect (elf_file_t ef, vm_prot_t prot)
 
Elf_Addr elf_relocaddr (linker_file_t lf, Elf_Addr x)
 
static const char * symbol_name (elf_file_t ef, Elf_Size r_info)
 
static int symbol_type (elf_file_t ef, Elf_Size r_info)
 
static unsigned long elf_hash (const char *name)
 
static int link_elf_lookup_symbol1 (linker_file_t lf, const char *name, c_linker_sym_t *sym, bool see_local)
 
static int link_elf_symbol_values1 (linker_file_t lf, c_linker_sym_t sym, linker_symval_t *symval, bool see_local)
 
const Elf_Sym * elf_get_sym (linker_file_t lf, Elf_Size symidx)
 
const char * elf_get_symname (linker_file_t lf, Elf_Size symidx)
 

Variables

static kobj_method_t link_elf_methods []
 
static struct linker_class link_elf_class
 
static bool link_elf_leak_locals = true
 
static struct elf_set_head set_pcpu_list
 
struct _dynamic _DYNAMIC
 
static unsigned long kern_relbase = KERNBASE
 

Macro Definition Documentation

◆ APPLY_RELOCS

#define APPLY_RELOCS (   iter,
  tbl,
  tblsize,
  type 
)
Value:
do { \
for ((iter) = (tbl); (iter) != NULL && \
(iter) < (tbl) + (tblsize) / sizeof(*(iter)); (iter)++) { \
if ((symbol_type(ef, (iter)->r_info) == \
STT_GNU_IFUNC || \
elf_is_ifunc_reloc((iter)->r_info)) != ifuncs) \
continue; \
if (reloc(&ef->lf, (Elf_Addr)ef->address, \
(iter), (type), lookup)) { \
symname = symbol_name(ef, (iter)->r_info); \
printf("link_elf: symbol %s undefined\n", \
symname); \
return (ENOENT); \
} \
} \
} while (0)
device_property_type_t type
Definition: bus_if.m:941

◆ LS_PADDING

#define LS_PADDING   0x90909090

Definition at line 654 of file link_elf.c.

◆ MAXSEGS

#define MAXSEGS   4

Definition at line 76 of file link_elf.c.

Typedef Documentation

◆ elf_file_t

typedef struct elf_file * elf_file_t

◆ elf_reloc_fn

typedef int(* elf_reloc_fn) (linker_file_t lf, Elf_Addr relocbase, const void *data, int type, elf_lookup_fn lookup)

Definition at line 202 of file link_elf.c.

Function Documentation

◆ __FBSDID()

__FBSDID ( "$FreeBSD$"  )

◆ elf_get_sym()

const Elf_Sym * elf_get_sym ( linker_file_t  lf,
Elf_Size  symidx 
)

Definition at line 1791 of file link_elf.c.

References elf_file::nchains, and elf_file::symtab.

◆ elf_get_symname()

const char * elf_get_symname ( linker_file_t  lf,
Elf_Size  symidx 
)

Definition at line 1801 of file link_elf.c.

References elf_file::nchains, elf_file::strtab, sym, and elf_file::symtab.

◆ elf_hash()

static unsigned long elf_hash ( const char *  name)
static

Definition at line 1488 of file link_elf.c.

References name.

Referenced by link_elf_lookup_symbol1().

Here is the caller graph for this function:

◆ elf_lookup()

static int elf_lookup ( linker_file_t  lf,
Elf_Size  symidx,
int  deps,
Elf_Addr *  res 
)
static

Definition at line 1820 of file link_elf.c.

References addr, elf_file::address, elf_set_find(), linker_file_lookup_symbol(), elf_file::nchains, res, set_pcpu_list, start, elf_file::strtab, sym, and elf_file::symtab.

Referenced by link_elf_reloc_local(), and relocate_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ elf_relocaddr()

Elf_Addr elf_relocaddr ( linker_file_t  lf,
Elf_Addr  x 
)

Definition at line 1338 of file link_elf.c.

References link_elf_class, elf_file::pcpu_base, and elf_file::pcpu_start.

◆ elf_set_add()

static void elf_set_add ( struct elf_set_head *  list,
Elf_Addr  start,
Elf_Addr  stop,
Elf_Addr  base 
)
static

Definition at line 217 of file link_elf.c.

References elf_set::es_start, elf_set::es_stop, malloc(), set, start, and stop.

Referenced by parse_dpcpu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ elf_set_delete()

static void elf_set_delete ( struct elf_set_head *  list,
Elf_Addr  start 
)
static

Definition at line 262 of file link_elf.c.

References elf_set::es_start, free(), set, and start.

Referenced by link_elf_unload_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ elf_set_find()

static int elf_set_find ( struct elf_set_head *  list,
Elf_Addr  addr,
Elf_Addr *  start,
Elf_Addr *  base 
)
static

Definition at line 244 of file link_elf.c.

References elf_set::es_start, elf_set::es_stop, set, and start.

Referenced by elf_lookup().

Here is the caller graph for this function:

◆ link_elf_debug_symbol_values()

static int link_elf_debug_symbol_values ( linker_file_t  lf,
c_linker_sym_t  sym,
linker_symval_t *  symval 
)
static

Definition at line 1628 of file link_elf.c.

References elf_file::address, elf_file::ddbstrtab, elf_file::ddbsymcnt, elf_file::ddbsymtab, link_elf_symbol_values1(), sym, and elf_file::symtab.

Referenced by link_elf_each_function_nameval(), and link_elf_symbol_values().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_each_function_name()

static int link_elf_each_function_name ( linker_file_t  file,
int(*)(const char *, void *)  callback,
void *  opaque 
)
static

Definition at line 1745 of file link_elf.c.

References callback, elf_file::ddbstrtab, elf_file::ddbsymtab, opaque, and symp.

◆ link_elf_each_function_nameval()

static int link_elf_each_function_nameval ( linker_file_t  file,
linker_function_nameval_callback_t  callback,
void *  opaque 
)
static

Definition at line 1766 of file link_elf.c.

References callback, elf_file::ddbsymtab, link_elf_debug_symbol_values(), opaque, and symp.

Here is the call graph for this function:

◆ link_elf_error()

static void link_elf_error ( const char *  filename,
const char *  s 
)
static

Definition at line 343 of file link_elf.c.

References filename, and printf().

Referenced by link_elf_load_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_init()

◆ link_elf_invoke_ctors()

static void link_elf_invoke_ctors ( caddr_t  addr,
size_t  size 
)
static

Definition at line 352 of file link_elf.c.

References addr.

Referenced by link_elf_link_common_finish().

Here is the caller graph for this function:

◆ link_elf_link_common_finish()

static int link_elf_link_common_finish ( linker_file_t  lf)
static

Definition at line 372 of file link_elf.c.

References elf_file::address, elf_file::dynamic, link_elf_invoke_ctors(), and malloc().

Referenced by link_elf_init(), link_elf_link_preload_finish(), and link_elf_load_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_link_preload()

static int link_elf_link_preload ( linker_class_t  cls,
const char *  filename,
linker_file_t *  result 
)
static

◆ link_elf_link_preload_finish()

static int link_elf_link_preload_finish ( linker_file_t  lf)
static

Definition at line 935 of file link_elf.c.

References link_elf_link_common_finish(), link_elf_preload_parse_symbols(), preload_protect(), and relocate_file().

Here is the call graph for this function:

◆ link_elf_load_file()

◆ link_elf_lookup_debug_symbol()

static int link_elf_lookup_debug_symbol ( linker_file_t  lf,
const char *  name,
c_linker_sym_t *  sym 
)
static

Definition at line 1566 of file link_elf.c.

References elf_file::ddbstrtab, elf_file::ddbsymtab, link_elf_lookup_symbol1(), name, sym, and symp.

Referenced by link_elf_lookup_symbol().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_lookup_set()

static int link_elf_lookup_set ( linker_file_t  lf,
const char *  name,
void ***  startp,
void ***  stopp,
int *  countp 
)
static

Definition at line 1692 of file link_elf.c.

References count, free(), link_elf_lookup_symbol(), link_elf_symbol_values(), malloc(), name, snprintf(), start, stop, and sym.

Referenced by parse_dpcpu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_lookup_symbol()

static int link_elf_lookup_symbol ( linker_file_t  lf,
const char *  name,
c_linker_sym_t *  sym 
)
static

Definition at line 1558 of file link_elf.c.

References link_elf_leak_locals, link_elf_lookup_debug_symbol(), link_elf_lookup_symbol1(), name, and sym.

Referenced by link_elf_lookup_set().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_lookup_symbol1()

static int link_elf_lookup_symbol1 ( linker_file_t  lf,
const char *  name,
c_linker_sym_t *  sym,
bool  see_local 
)
static

Definition at line 1504 of file link_elf.c.

References elf_file::buckets, elf_file::chains, elf_hash(), name, elf_file::nbuckets, elf_file::nchains, printf(), elf_file::strtab, sym, symp, and elf_file::symtab.

Referenced by link_elf_lookup_debug_symbol(), and link_elf_lookup_symbol().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_preload_parse_symbols()

static int link_elf_preload_parse_symbols ( elf_file_t  ef)
static

Definition at line 513 of file link_elf.c.

References elf_file::ddbstrcnt, elf_file::ddbstrtab, elf_file::ddbsymcnt, elf_file::ddbsymtab, elf_file::modptr, preload_search_info(), printf(), strtab, and symtab.

Referenced by link_elf_init(), and link_elf_link_preload_finish().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_reloc_local()

static void link_elf_reloc_local ( linker_file_t  lf)
static

Definition at line 1881 of file link_elf.c.

References elf_file::address, elf_lookup(), elf_file::rel, elf_file::rela, elf_file::relasize, and elf_file::relsize.

Referenced by link_elf_link_preload(), and link_elf_load_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_search_symbol()

static int link_elf_search_symbol ( linker_file_t  lf,
caddr_t  value,
c_linker_sym_t *  sym,
long *  diffp 
)
static

Definition at line 1653 of file link_elf.c.

References elf_file::address, elf_file::ddbsymtab, diffp, sym, and value.

◆ link_elf_strtab_get()

static long link_elf_strtab_get ( linker_file_t  lf,
caddr_t *  strtab 
)
static

Definition at line 1925 of file link_elf.c.

References elf_file::ddbstrcnt, elf_file::ddbstrtab, and strtab.

◆ link_elf_symbol_values()

static int link_elf_symbol_values ( linker_file_t  lf,
c_linker_sym_t  sym,
linker_symval_t *  symval 
)
static

Definition at line 1619 of file link_elf.c.

References link_elf_debug_symbol_values(), link_elf_leak_locals, link_elf_symbol_values1(), and sym.

Referenced by link_elf_lookup_set().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ link_elf_symbol_values1()

static int link_elf_symbol_values1 ( linker_file_t  lf,
c_linker_sym_t  sym,
linker_symval_t *  symval,
bool  see_local 
)
static

Definition at line 1595 of file link_elf.c.

References elf_file::address, elf_file::nchains, elf_file::strtab, sym, elf_file::symtab, and symtab.

Referenced by link_elf_debug_symbol_values(), and link_elf_symbol_values().

Here is the caller graph for this function:

◆ link_elf_symtab_get()

static long link_elf_symtab_get ( linker_file_t  lf,
const Elf_Sym **  symtab 
)
static

Definition at line 1912 of file link_elf.c.

References elf_file::ddbsymcnt, elf_file::ddbsymtab, and symtab.

◆ link_elf_unload_file()

◆ link_elf_unload_preload()

static void link_elf_unload_preload ( linker_file_t  file)
static

Definition at line 1406 of file link_elf.c.

References preload_delete_name().

Referenced by link_elf_unload_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_dpcpu()

static int parse_dpcpu ( elf_file_t  ef)
static

Definition at line 656 of file link_elf.c.

References dpcpu_alloc(), dpcpu_copy(), elf_set_add(), elf_file::lf, link_elf_lookup_set(), LS_PADDING, elf_file::pcpu_base, elf_file::pcpu_start, elf_file::pcpu_stop, printf(), set_pcpu_list, and uprintf().

Referenced by link_elf_link_preload(), and link_elf_load_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_dynamic()

◆ preload_protect()

static int preload_protect ( elf_file_t  ef,
vm_prot_t  prot 
)
static

Definition at line 781 of file link_elf.c.

References elf_file::address.

Referenced by link_elf_link_preload(), and link_elf_link_preload_finish().

Here is the caller graph for this function:

◆ relocate_file()

static int relocate_file ( elf_file_t  ef)
static

Definition at line 1473 of file link_elf.c.

References elf_lookup(), and relocate_file1().

Referenced by link_elf_link_preload_finish(), and link_elf_load_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ relocate_file1()

static int relocate_file1 ( elf_file_t  ef,
elf_lookup_fn  lookup,
elf_reloc_fn  reloc,
bool  ifuncs 
)
static

Definition at line 1438 of file link_elf.c.

References APPLY_RELOCS, elf_file::pltrel, elf_file::pltrela, elf_file::pltrelasize, elf_file::pltrelsize, elf_file::rel, elf_file::rela, elf_file::relasize, and elf_file::relsize.

Referenced by relocate_file().

Here is the caller graph for this function:

◆ symbol_name()

static const char * symbol_name ( elf_file_t  ef,
Elf_Size  r_info 
)
static

Definition at line 1414 of file link_elf.c.

References elf_file::strtab, and elf_file::symtab.

Referenced by mi_startup().

Here is the caller graph for this function:

◆ symbol_type()

static int symbol_type ( elf_file_t  ef,
Elf_Size  r_info 
)
static

Definition at line 1426 of file link_elf.c.

References elf_file::symtab.

◆ SYSCTL_BOOL()

SYSCTL_BOOL ( _debug  ,
OID_AUTO  ,
link_elf_leak_locals  ,
CTLFLAG_RWTUN  ,
link_elf_leak_locals,
,
"Allow local symbols to participate in global module symbol resolution"   
)

◆ SYSCTL_ULONG() [1/2]

SYSCTL_ULONG ( _kern  ,
OID_AUTO  ,
base_address  ,
CTLFLAG_RD  ,
SYSCTL_NULL_ULONG_PTR  ,
KERNBASE  ,
"Kernel base address"   
)

◆ SYSCTL_ULONG() [2/2]

SYSCTL_ULONG ( _kern  ,
OID_AUTO  ,
relbase_address  ,
CTLFLAG_RD  ,
kern_relbase,
,
"Kernel relocated base address"   
)

◆ SYSINIT()

SYSINIT ( link_elf  ,
SI_SUB_KLD  ,
SI_ORDER_THIRD  ,
link_elf_init  ,
NULL   
)

◆ TAILQ_HEAD()

TAILQ_HEAD ( elf_set_head  ,
elf_set   
)

Variable Documentation

◆ _DYNAMIC

struct _dynamic _DYNAMIC
extern

Referenced by link_elf_init().

◆ kern_relbase

unsigned long kern_relbase = KERNBASE
static

Definition at line 420 of file link_elf.c.

Referenced by link_elf_init().

◆ link_elf_class

struct linker_class link_elf_class
static
Initial value:
= {
"elf32",
link_elf_methods, sizeof(struct elf_file)
}

Definition at line 188 of file link_elf.c.

Referenced by elf_relocaddr(), link_elf_init(), link_elf_link_preload(), and link_elf_load_file().

◆ link_elf_leak_locals

bool link_elf_leak_locals = true
static

Definition at line 197 of file link_elf.c.

Referenced by link_elf_lookup_symbol(), and link_elf_symbol_values().

◆ link_elf_methods

kobj_method_t link_elf_methods[]
static
Initial value:
= {
KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
KOBJMETHOD(linker_lookup_debug_symbol, link_elf_lookup_debug_symbol),
KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
KOBJMETHOD(linker_unload, link_elf_unload_file),
KOBJMETHOD(linker_link_preload, link_elf_link_preload),
KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
KOBJMETHOD_END
}
static int link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
Definition: kern_ctf.c:42
int linker_ctf_get(linker_file_t file, linker_ctf_t *lc)
Definition: kern_linker.c:777
static int linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval)
Definition: kern_linker.c:969
static int linker_load_file(const char *filename, linker_file_t *result)
Definition: kern_linker.c:432

Definition at line 169 of file link_elf.c.

◆ set_pcpu_list

struct elf_set_head set_pcpu_list
static

Definition at line 211 of file link_elf.c.

Referenced by elf_lookup(), link_elf_init(), link_elf_unload_file(), and parse_dpcpu().