Commit graph

127 commits

Author SHA1 Message Date
John Baldwin 257f36a7db kldxref: Properly handle reading strings near the end of an ELF file
If a string is at or near the end of an input file and the amount of
remaining data in the file is smaller than the maximum string size,
the pread(2) system call would return a short read which is treated as
an error.  Instead, add a new helper function for reading a string
which permits short reads so long as the data read from the file
contains a terminated string.

Reported by:	jrtc27
Reviewed by:	jrtc27
Sponsored by:	University of Cambridge, Google, Inc.
Differential Revision:	https://reviews.freebsd.org/D44419

(cherry picked from commit 785600d0fb)
2024-04-08 11:06:41 -07:00
John Baldwin cad6775d96 kldxref: Workaround incorrect PT_DYNAMIC in existing powerpc kernels
Existing powerpc kernels include additional sections beyond .dynamic
in the PT_DYNAMIC segment.  Relax the requirement for an exact size
match of the section and segment for PowerPC files as a workaround.

Reported by:	jrtc27
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D43123

(cherry picked from commit 6631e2f9b4)
2024-01-18 13:32:10 -08:00
John Baldwin fb4c8b2d89 kldxref: Correct contract number in license ACK
Reviewed by:	brooks
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D43371

(cherry picked from commit 78258ee013)
2024-01-18 13:32:04 -08:00
Alex Richardson b13e59714f kldxref: fix bootstrapping on Linux with Clang 16
The glibc fts_open() callback type does not have the second const
qualifier and it appears that Clang 16 errors by default for mismatched
function pointer types. Add an ifdef to handle this case.

(cherry picked from commit 30887c7d48)
2024-01-18 13:31:52 -08:00
John Baldwin 319092aaac kldxref: Appease a Coverity warning
While parsing .dynamic, nsym is set when parsing the symbol table from
.dynsym.  That parsing also sets ef->ef_symtab to a non-NULL value.
The value of nsym isn't validated until after a check for
ef->ef_symtab being NULL, so nsym always has a valid value when it is
read.  However, that chain of events is a bit much for static analysis
to follow, so initialize nsym to 0 before parsing sections to quiet
the warning.

Reported by:	Coverity Scan
CID:		1532339
Sponsored by:	DARPA

(cherry picked from commit d281fece43)
2024-01-18 13:31:39 -08:00
John Baldwin 21dec6c48f kldxref: Simplify handling of ELF object files
Unlike the backend for ELF DSOs, the object file backend allocated an
aligned chunk of memory and read all of the in-memory sections from
the file into this memory even though most of the file contents were
never used.  Instead, just track a set of virtual addresses (based at
0) that each loaded section would be loaded at and only read the
necessary bits from the backing file when needed.

Reviewed by:	imp
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D43126

(cherry picked from commit 0d557cdf6f)
2024-01-18 13:31:32 -08:00
John Baldwin ea71737682 kldxref: Simplify elf_read_raw_data
Use pread as a valid offset is always passed now.  Originally the DSO
code read the .hash section in two separate requests and relied on the
implicit offset for the second read, but now the hash table is fetched
in a single call.

Reviewed by:	imp
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D43125

(cherry picked from commit ed96fd7fc6)
2024-01-18 13:31:22 -08:00
John Baldwin f7e181afba kldxref: Tidy error handling for invalid addresses
- Don't print an offset value that's always zero.

- ef_get_offset can't return -1.

Reviewed by:	imp
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D43124

(cherry picked from commit 6d46e2e0a7)
2024-01-18 13:31:14 -08:00
Jessica Clarke 1dde3d8d7b kldxref: Reduce divergence between per-architecture files
Note that relbase is always 0 for DSOs so its omission for __KLD_SHARED
architectures was not a bug in practice.

Whilst here, also parenthesise the dest offset for where to avoid
transiently creating an out-of-bounds pointer, which is UB (though even
on CHERI architectures, where capability bounds compression can result
in that creating invalid capabilities that will trap on dereference,
optimisation will reassociate to the correct form in practice and thus
work just fine).

(cherry picked from commit 2a622f14e8)
2024-01-18 13:31:00 -08:00
Jessica Clarke 09371fa662 kldxref: Implement aarch64 R_AARCH64_AB64 relocation
Doing nothing seems to be sufficient but is strange, inconsistent with
other architectures, and not necessary when it's easy to support
properly.

(cherry picked from commit aaba1490ec)
2024-01-18 13:30:49 -08:00
Jessica Clarke 8b3c539a20 kldxref: Fix MIPS Elf_Rel addend computation
Fixes:	0299afdff1 ("kldxref: Make use of libelf to be a portable cross tool")
(cherry picked from commit ae7925cafe)
2024-01-18 13:30:40 -08:00
Jessica Clarke 3ab67201a2 kldxref: Add arm support
(cherry picked from commit c88f7d8fb5)
2024-01-18 13:30:32 -08:00
Jessica Clarke 8db082ca31 kldxref: Put each ef_CPUARCH.c on its own line to prepare for ef_arm.c
Adding ef_arm.c would push this past 80 columns, so prepare for its
addition by splitting the line up.

(cherry picked from commit ada9d12f95)
2024-01-18 13:30:23 -08:00
Jessica Clarke e33e621428 kldxref: Fix for 32-bit powerpc
R_PPC_RELATIVE lost its 32-bit handling in the libelf conversion.

Reported by:	bapt
Fixes:		0299afdff1 ("kldxref: Make use of libelf to be a portable cross tool")

(cherry picked from commit d1ce87ae0d)
2024-01-18 13:30:15 -08:00
John Baldwin d18406a120 kldxref: Cast rtype to int in ef_aarch64.c in warning
This is consistent with the other architecture-specific relocation
handlers.

Reported by:	mjg
Sponsored by:	DARPA

(cherry picked from commit 894f3f48c3)
2024-01-18 13:30:06 -08:00
Baptiste Daroussin a77717659b kldxref: fix build on 32 bits arches
(cherry picked from commit 97530894fc)
2024-01-18 13:29:42 -08:00
John Baldwin 8054480839 kldxref: Make use of libelf to be a portable cross tool
This allows kldxref to operate on kernel objects from any
architecture, not just the native architecture.  In particular, this
will permit generating linker.hints files as part of a cross-arch
release build.

- elf.c is a new file that includes various wrappers around libelf
  including routines to read ELF data structures such as program and
  section headers and ELF relocations into the "generic" forms
  described in <gelf.h>.  This file also provides routines for
  converting a linker set into an array of addresses (GElf_Addr)
  as well as reading architecture-specific mod_* structures and
  converting them into "generic" Gmod_* forms where pointers are
  replaced with addresses.

- The various architecture-specific reloc handlers now use GElf_*
  types for most values (including GElf_Rel and GElf_Rela for
  relocation structures) and use routines from <sys/endian.h> to read
  and write target values.  A new linker set matches reloc handlers
  to specific ELF (class, encoding, machine) tuples.

- The bits of kldxref.c that write out linker.hints now use the
  encoding (ELFDATA2[LM]SB) of the first file encountered in a
  directory to set the endianness of the output file.  Input files
  with a different architecture in the same directory are skipped with
  a warning.  In addition, the initial version record for the file
  must be deferred until the first record is finished since the
  architecture of the output file is not known until then.

- Various places that used 'sizeof(void *)' throughout now use
  'elf_pointer_size()' to determine the size of a pointer in the
  target architecture.

Tested by:	amd64 binary on both amd64 and i386 /boot/kernel
Reviewed by:	imp
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D42966

(cherry picked from commit 0299afdff1)
2024-01-18 13:29:12 -08:00
John Baldwin b2cc5745dc kldxref: Refactor PNP entry parsing, no functional change
- Add a free_pnp_list to complement parse_pnp_list.  Add freeing
  of 'new_desc' which was previously leaked.

- Move body of loop that checked a single pnp list element against a
  table entry into a parse_pnp_entry function to reduce indentation
  and split parse_entry into a smaller function.

- Similarly, split out a record_pnp_info function from parse_entry
  which builds the pnp_list and walks a table.

Reviewed by:	imp
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D42965

(cherry picked from commit c40fa3dc98)
2024-01-18 13:24:53 -08:00
John Baldwin 7da77f0266 Stop #defining FREEBSD_ELF
This was originally used (along with FREEBSD_AOUT) to prefer the use
of ELF in various tools instead of a.out as part of the a.out to ELF
transition in the 3.x days.  The last use of it was removed from
<link.h> in commit 66422f5b7a back in
2002, but various files still #define it.

Reviewed by:	kevans, imp, emaste
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D42964

(cherry picked from commit 4a3cf5f329)
2024-01-04 16:28:53 -08:00
John Baldwin de09d07a10 kldxref: Add a copyright and license statement to ef.h
This uses the statement from other files in kldxref when the tool was
first imported in commit 9c6f92408c.

Reviewed by:	kevans
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D42963

(cherry picked from commit 087c4c90d1)
2024-01-04 16:28:39 -08:00
Warner Losh 350f9ac5b3 Remove $FreeBSD$: one-line bare tag
Remove /^\s*\$FreeBSD\$$\n/

Similar commit in main:
(cherry picked from commit 05248206f7)
2023-08-23 11:43:33 -06:00
Warner Losh b144e70a33 Remove $FreeBSD$: two-line nroff pattern
Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/

Similar commit in main:
(cherry picked from commit fa9896e082)
2023-08-23 11:43:31 -06:00
Warner Losh 023fc80ee3 Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/

Similar commit in main:
(cherry picked from commit d0b2dbfa0e)
2023-08-23 11:43:30 -06:00
Warner Losh 3d497e17eb Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/

Similar commit in main:
(cherry picked from commit 1d386b48a5)
2023-08-23 11:43:26 -06:00
Warner Losh 8ad303d68c Remove $FreeBSD$: one-line .c comment pattern
Remove /^/[*/]\s*\$FreeBSD\$.*\n/

Similar commit in main:
(cherry picked from commit 2a63c3be15)
2023-08-23 11:43:23 -06:00
Warner Losh 17da660ad5 Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/

Similar commit in main:
(cherry picked from commit b3e7694832)
2023-08-23 11:43:21 -06:00
Warner Losh caa41f6417 spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with:		pfg
MFC After:		3 days
Sponsored by:		Netflix

(cherry picked from commit 4d846d260e)
2023-07-25 09:13:49 -06:00
Mitchell Horne 9f810201f0 kldxref: handle R_RISCV_64 relocation
These are emitted in at least two kmods, and kldxref prints a warning.
While here, remove the unneeded local variable 'val'.

Reviewed by:	jrtc27, imp, emaste
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D37038

(cherry picked from commit fc4c39c54f)
2022-10-28 11:10:37 -03:00
Alfredo Dal'Ava Junior 0d27ad33a4 kldxref: add R_PPC_ADDR32 handler
Implements R_PPC_ADDR32 handler for kldxref, addressing
"kldxref: unhandled relocation type 1" when running
"kldxref /boot/kernel" on powerpc 32 bits.

Reviewed by:	luporl
MFC after:	2 days
Sponsored by:	Instituto de Pesquisas Eldorado (eldorado.org.br)
Differential Revision:	https://reviews.freebsd.org/D33745

(cherry picked from commit 79f5d19890)
2022-02-17 18:48:52 -03:00
Alfredo Dal'Ava Junior 04d4de5e60 kldxref: add R_PPC64_ADDR64 handler
Implements R_PPC64_ADDR64 handler for kldxref, addressing
"kldxref: unhandled relocation type 38" when running
"kldxref /boot/kernel"

Reviewed by:	jhibbits
MFC after:	1 week
Sponsored by:	Instituto de Pesquisas Eldorado (eldorado.org.br)
Differential Revision:	https://reviews.freebsd.org/D33615

(cherry picked from commit c3d9c63329)
2022-02-07 19:42:16 -03:00
Konstantin Belousov 2585f892e0 kldxref: do not error out if specified path is not directory, for -d mode
(cherry picked from commit d7751071bc)
2021-05-27 03:33:10 +03:00
Jessica Clarke a98fa52ec6 kldxref: Avoid buffer overflows in parse_pnp_list
We convert a string like "W32:vendor/device" into "I:vendor;I:device",
where the output is longer than the input, but only allocate space equal
to the length of the input, leading to a buffer overflow.

Instead use open_memstream so we get a safe dynamically-grown buffer.

Found by:	CHERI
Reviewed by:	imp, jhb (mentor)
Approved by:	imp, jhb (mentor)
Obtained from:	CheriBSD
Differential Revision:	https://reviews.freebsd.org/D26637
2020-10-15 18:03:14 +00:00
Dimitry Andric 05c312a339 Bump kldxref's MAXSEGS to 16, to stop complaints about the kernel
supposedly having too many segments, when lld 11 links it. Such kernels
should load just fine.

Note that we may still do some tweaking of our kernel linker scripts, to
lower the number of segments, although the exact benefit is not entirely
clear.
2020-08-20 18:50:46 +00:00
Yoshihiro Takahashi 8f11c99715 - Cleanups related to sparc64 removal.
- Remove remains of sparc64 files.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D25831
2020-07-28 10:58:37 +00:00
Emmanuel Vadot a7b5a3d486 pkgbase: Put a lot of binaries and lib in FreeBSD-runtime
All of them are needed to be able to boot to single user and be able
to repair a existing FreeBSD installation so put them directly into
FreeBSD-runtime.

Reviewed by:    bapt, gjb
Differential Revision:  https://reviews.freebsd.org/D21503
2019-09-05 14:13:08 +00:00
Conrad Meyer 9c1fa7a429 kldxref(8): Sort MDT_MODULE info first in linker.hints output
MDT_MODULE info is required to be ordered before any other MDT metadata for
a given kld because it serves as an implicit record boundary between
distinct klds for linker.hints consumers.  kldxref(8) has previously relied
on the assumption that MDT_MODULE was ordered relative to other module
metadata in kld objects by source code ordering.

However, C does not require implementations to emit file scope objects in
any particular order, and it seems that GCC 6.4.0 and/or binutils 2.32 ld
may reorder emitted objects with respect to source code ordering.

So: just take two passes over a given .ko's module metadata, scanning for
the MDT_MODULE on the first pass and the other metadata on subsequent
passes.  It's not super expensive and not exactly a performance-critical
piece of code.  This ensures MDT_MODULE is always ordered before
MDT_PNP_INFO and other MDTs, regardless of compiler/linker movement.  As a
fringe benefit, it removes the requirement that care be taken to always
order MODULE_PNP_INFO after DRIVER_MODULE in source code.

Reviewed by:	emaste, imp
Differential Revision:	https://reviews.freebsd.org/D20405
2019-05-27 17:33:20 +00:00
John Baldwin c1bba4445c Parse MIPS relocations to unbreak kldxref on MIPS.
Parse the R_MIPS_32 and R_MIPS_64 relocations.  Both Elf_Rel and
Elf_Rela relocations are handled since O32 MIPS uses Elf_Rel while N64
uses Elf_Rela.  Note that R_MIPS_32 is only handled for 32-bit mips
and R_MIPS_64 for 64-bit.  N32 is untested.

Reviewed by:	imp
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D19870
2019-04-24 23:35:10 +00:00
Leandro Lupori 7e4520854c Fix kldxref on PowerPC64
When using kldxref on kernel modules built with clang8 + lld8,
kldxref would be unable to find the modules metadata information,
because PowerPC64 was using the ef_nop.c implementation of
ef_reloc().

When GNU LD was used, it was also relocating the metadata section of
the .ko file. LLD does not do this, but only generate dynamic
relocations for it. With minor changes, ef_powerpc.c can now work
for PowerPC64 too.

Reviewed by:	emaste
Differential Revision:	https://reviews.freebsd.org/D19370
2019-02-27 13:24:42 +00:00
Ed Maste b6274d1775 kldxref: use appropriate Elf_Off type for offsets
Submitted by:	Mitchell Horne <mhorne063@gmail.com>
Reviewed by:	kib
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D13957
2018-11-09 15:02:53 +00:00
John Baldwin 1bcac4ba99 Add support for relocations in RISC-V kernel modules to kldxref.
Reviewed by:	br, markj
Approved by:	re (gjb)
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D17415
2018-10-08 19:15:58 +00:00
Warner Losh 517a1827f7 Relax syntax requirements of source string
The original spec for the strings describing the PNP info didn't allow
spaces. Several times now people have broken the install by including
them. Relax the syntax to allow them after the ; which is where
people's muscle memory tends to put them.

Approved by: re@ (gjb)
2018-09-26 19:41:06 +00:00
Warner Losh ee150a3376 Use EF_SEG_READ_STRING instead of EF_SEG_READ when reading strings.
Normally, we can get away with just reading the 1k buffer for the
string, since the placement of the data is generally no where near the
end of the file. However, it's possible that the string is within the
last 1k of the file, in which case the read will fail, and we'll not
produce the proper records needed for devmatch to work. By reading
using EF_SEG_READ_STRING, we automatically work around these problems
while still retaining safety.

This fix a problem with devmatch where we wouldn't load certain
modules (like ums). This didn't always happen (my tree didn't exhibit
it, while nathan's did because his optimization options were more
agressive).

Reported by: nathanw@
2018-07-15 05:29:39 +00:00
Warner Losh a35ddacab7 Fixup minor nits in the PNP_INFO protocol.
Sponsored by: Netflix
2018-02-17 06:57:03 +00:00
John Baldwin 66f8917bea Adjust format string to fix build. 2018-01-18 00:24:05 +00:00
John Baldwin 449af04fc9 Tidy some whitespace. 2018-01-18 00:23:11 +00:00
Ed Maste 559d4a5216 kldxref: additional sytle(9) cleanup
Reported by:	kib (via comments in D13957)
Sponsored by:	The FreeBSD Foundation
2018-01-17 20:43:30 +00:00
Ed Maste e2d0802c6b kldxref: improve style(9)
Address style issues including some previously raised in D13923.

- Use designated initializers for structs
- Always use bracketed return style
- No initialization in declarations
- Align function prototype names
- Remove old commented code/unused includes

Submitted by:	Mitchell Horne <mhorne063@gmail.com>
Reviewed by:	kib
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D13943
2018-01-17 19:59:43 +00:00
Ed Maste da67e6e62c kldxref: handle modules with md_cval at the end of allocated sections
Attempting to retrieve an md_cval string from a kernel module with
kldxref would throw a offset error for modules created using lld, since
this value would be placed at the end of all allocated sections.

Add an ef_read_seg_string method to the ef interface, to allow reading
strings of varying size without attempting to read beyond the segment's
bounds.

PR:		224875
Submitted by:	Mitchell Horne <mhorne063@gmail.com>
Reviewed by:	cem, kib
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D13923
2018-01-16 18:20:12 +00:00
Ed Maste f68e716ffe kldxref: correct function names in warning messages
Sponsored by:	The FreeBSD Foundation
2018-01-02 21:31:54 +00:00
Pedro F. Giffuni 1de7b4b805 various: general adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using misidentified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

No functional change intended.
2017-11-27 15:37:16 +00:00