2

I need to perform objdump on an ELF to get address and/or sizes of specific functions and variables as well objcopy to convert it to a different format for operations performed later. Especially with the first part, some of the operations done on the result of objdump are a bit complex, so I went with creating a small C++ application to perform that. It works, however it currently calls the gcc's objdump and objdopy directly including its parameters with a system call.

There are multiple reasons why I don't like this approach, main one of which being that I either need to rely on user having them on the PC or distribute them with my own executable and treat it as a kind of dynamic library, but in form of executables, which I don't like either.

My question is, is there some form of (gcc?) library that I could (preferably statically) link to perform same operations that way? I'd really rather use something as close do gcc, especially when it comes to compatibility with gcc produced files as input and giving same output, as I already have that kind of version of my own executable working.

J_S
  • 2,985
  • 3
  • 15
  • 38
  • What is the actual problen you are trying to solve? *Why* do you do/need this? – Jesper Juhl May 21 '17 at 20:53
  • So you basically want to rewrite parts of `objdump/objcpy`? Then you'll probably want to take a look at [libbfd](ftp://ftp.gnu.org/old-gnu/Manuals/bfd-2.9.1/html_mono/bfd.html) – kaylum May 21 '17 at 20:55
  • @JesperJuhl I thought this is not relevant to the discussion but as you've asked I guess I'll answer. This is because I'm working on a embedded (bare-metal) project that requires performing some initial steps before starting (or debugging) the application. In particular, I need to load a portion of the binary into an external memory, which is connected to the main MCU. This is easiest done by a loader temporarily placed inside the MCU's RAM. However, PC that loads the target application (to both internal and external memory) needs to know the location of the loader functions, hence `objdump`. – J_S May 21 '17 at 21:01
  • @kaylum Not really rewrite it. I basically to do exactly what they already do, just not call an external executable from my executable, but have a library to do those tasks instead (preferably doing it _exactly_ the same way). – J_S May 21 '17 at 21:04

2 Answers2

4

All of those obj* tools are based on the BFD library. If you look at how objdump does its thing and copy it, you should be able to do the same in your own code.

Another thing that you can do is just copy the code from objdump and friends into your own programs. It's open source and you can do that. All you need is to make your own programs also GPL, which they'd have to be anyway to use the BFD library.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
2

The programs objdump and objcopy are not part of gcc, they are part of binutils. Speaking of which: libbfd and libopcodes are also part of binutils, and those contain functions you might be able to use.

Cheatah
  • 1,825
  • 2
  • 13
  • 21