26

I would like to rename symbols inside object files (.o) with something that would be the Mac equivalent of binutils' objcopy --redefine-syms tool.

I found no arm-apple-darwin10-objcopy. I tried the MacPorts' arm-elf-binutils port and also tried to play a bit with otool and segedit without much success.

Any ideas please?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • Are you OK with defining aliases, which means that older symbols will still exists ? – Laurent G Mar 28 '11 at 15:28
  • 2
    how about `brew install binutils` and `gobjcopy`? eg: `gobjcopy --redefine-sym _D4main9foo_test1FZv=_foobar main.o main2.o`; NOTE: the resulting executable seems wrong but `nm main2.o | grep _foobar` works – timotheecour Jan 25 '18 at 23:02

3 Answers3

5

Sounds like a job for Agner Fog's objconv.

From the announcement:

I have now finished making full support for Mach-O files in the object file converter mentioned in my previous posts. You may use it as a replacement for the missing objcopy utility.

Objconv can be used for the following purposes:
Convert object files and library/archive files between Mach-O, ELF, COFF and OMF formats for all x86 and x86-64 platforms.

Change symbol names in object files, make symbols weak, add alias names to symbols.

Build, modify and convert static library files (*.a, *.lib) across platforms (Mac, Linux, BSD, Windows)

Dump object files and executable files

Disassemble object files and executable files. Very good disassembler.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
  • from the FAQ: "11.3 Is it possible to convert files for ARM? No. A lot of people have asked about this, so there is obviously a need for such a tool, but I am not gonna make it. Objconv supports only files for x86 and x86-64 architectures. It will require a major rewrite of objconv to make a converter for ARM files." – Stéphane Jul 25 '13 at 18:41
3

I know I'm resurrecting this post from the dead, but...

I have a sudden need to do this as well, and discovering that objcopy doesn't work on OSX was a bit of a shock. But I think it's possible to use ld to achieve the same effect:

ld -r input.o -o output.o -alias oldsymbol newsymbol -unexported_symbol oldsymbol

This really just creates an alias for the symbol under a new name and hides the old one.

I haven't had a chance to do much testing yet, but looking at the output file with nm shows it seems to be doing the right thing.

David Given
  • 13,277
  • 9
  • 76
  • 123
  • This command deletes all debug symbols. Do you know how to copy them from the original object file? – Dmitry Jul 23 '13 at 12:11
  • 2
    `objcopy` can rename symbols T (text section symbol) and U (undefined) symbols. However, your `ld` solution can only handle the former. – Stephen Quan Feb 27 '14 at 00:11
1

objconv does not currently work for ARM, so this option is not available for iPhone. It should be no problem to use objconv from elf to mach-o for mac osx x86/x64 though. Let me know if you found a solution for ARM

iOSThing
  • 43
  • 6