This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy]Add support for *-freebsd output formats
ClosedPublic

Authored by jhenderson on Mar 21 2019, 8:12 AM.

Details

Summary

GNU objcopy can support output formats like elf32-i386-freebsd and elf64-x86-64-freebsd. The only difference from their regular non-freebsd counterparts that I have observed is that the freebsd versions set the OS/ABI field to ELFOSABI_FREEBSD. This patch sets the OS/ABI field accordingly whenever --output-format is specified.

Diff Detail

Repository
rL LLVM

Event Timeline

jhenderson created this revision.Mar 21 2019, 8:12 AM
rupprecht accepted this revision.Mar 21 2019, 9:59 AM

The objcopy on my machine doesn't seem to be built w/ the freebsd bfd targets enabled, so I can't repro, but this seems fine. Two interesting things I found while comparing against the source:

  1. For older FreeBSDs (<= 4.0), objcopy actually copies the string "FreeBSD" into e_ident [1]. This seems to work because there's enough padding bytes. (I suggest we don't do this, but I found it to be interesting trivia).
  2. In the method that actually sets the OSABI member [2], if the OSABI field is still ELFOSABI_NONE, it will change it to ELFOSABI_GNU if the object contains GNU symbols. Does this seem useful to do?
12065   /* To make things simpler for the loader on Linux systems we set the
12066      osabi field to ELFOSABI_GNU if the binary contains symbols of
12067      the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding.  */
12068   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
12069       && elf_tdata (abfd)->has_gnu_symbols)
12070     i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;

[1] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=bfd/elf32-i386.c;h=79cd8c65bc150781b9783cfc591b08fc36785890;hb=HEAD#l4414
[2] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=bfd/elf.c;h=73fb86971f3dcc0461ba17d5d24d3cf7add41761;hb=HEAD#l12055

This revision is now accepted and ready to land.Mar 21 2019, 9:59 AM

In the method that actually sets the OSABI member [2], if the OSABI field is still ELFOSABI_NONE, it will change it to ELFOSABI_GNU if the object contains GNU symbols. Does this seem useful to do?

It does seem somewhat useful, but it looks like a different, only tangetially-related, behaviour, so I'll file a bug for that, especially as I imagine it will slow down the tool (it'll have to keep track of all symbols that are GNU symbols).

grimar accepted this revision.Mar 22 2019, 2:46 AM

LGTM too.

grimar added inline comments.Mar 22 2019, 2:48 AM
tools/llvm-objcopy/ELF/ELFObjcopy.cpp
229

Oh. Was this place tested? Seems not.

jhenderson marked an inline comment as done.Mar 22 2019, 2:50 AM
jhenderson added inline comments.
tools/llvm-objcopy/ELF/ELFObjcopy.cpp
229

Yeah, I noticed this when I was uploading this patch. I've got a note on my desk to look at adding a test for this section (the Machine line immediately above it is also untested). Given the block is already untested, are you happy for this to be tested in a separate patch, or would you prefer it being part of this one?

grimar added inline comments.Mar 22 2019, 2:53 AM
tools/llvm-objcopy/ELF/ELFObjcopy.cpp
229

Given the block is already untested, are you happy for this to be tested in a separate patch, or would you prefer it being part of this one?

I think it's fine to do in a separate patch then.

This revision was automatically updated to reflect the committed changes.

I've filed https://bugs.llvm.org/show_bug.cgi?id=41196 for the GNU OSABI issue, and created D59691 for the test coverage addition.