GrammaTech / ddisasm

A fast and accurate disassembler
https://grammatech.github.io/ddisasm/
GNU Affero General Public License v3.0
645 stars 60 forks source link

[ls] fails reassembly #58

Closed gogo2464 closed 1 year ago

gogo2464 commented 1 year ago

ls.zip Please provide as much of the following information as possible: ddisasm --version 1.6.0 (cfa30e80 2023-03-21) ARM64+IA32+X64+ARM32+MIPS32

$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.6 LTS Release: 20.04 Codename: focal

I can not reassemble the ls program with gcc.

$ ddisasm ls --asm ls.s Building the initial gtirb representation [ 1ms] Processing module: ls disassembly load [ 359ms] compute [ 3s] transform [ 206ms] SCC analysis compute [ 9ms] transform [ 0ms] no return analysis load [ 11ms] compute [ 294ms] transform [ 0ms] function inference load [ 35ms] compute [ 19ms] transform [ 7ms] Printing assembler [ 150ms]

$ gcc -nostartfiles ls.s -o ls-out /usr/bin/ld: warning: cannot create .note.gnu.build-id section, --build-id ignored /usr/bin/ld: /tmp/cchpOuqm.o: in function FUN_16800': (.text+0x11a76): undefined reference tofreecon' /usr/bin/ld: /tmp/cchpOuqm.o: in function FUN_16870': (.text+0x11aba): undefined reference togetfilecon' /usr/bin/ld: /tmp/cchpOuqm.o: in function FUN_16890': (.text+0x11ada): undefined reference tolgetfilecon' /usr/bin/ld: /tmp/cchpOuqm.o: in function FUN_168b0': (.text+0x11afa): undefined reference tofgetfilecon' /usr/bin/ld: /tmp/cchpOuqm.o: in function FUN_6f20': (.text+0x21ae): undefined reference tofreecon' collect2: error: ld returned 1 exit status

adamjseitz commented 1 year ago

Hi @gogo2464,

Most often, undefined reference errors occur when you need to link with an additional library when reassembling.

In this case, you need libselinux. You can find the needed libraries with readelf on the original binary:

$ readelf --dynamic ls
Dynamic section at offset 0x21a58 contains 28 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libselinux.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
...

You may need to install libselinux1-dev if you're on Ubuntu, and this command should work:

gcc -nostartfiles ls.s -o ls-out -lselinux

If you generate a GTIRB file with ddisasm, and use gtirb-pprinter's binary printing mode to reassemble, it takes care of adding the right libraries:

ddisasm --ir ls.gtirb ls
gtirb-pprinter --binary ls-out ls.gtirb
gogo2464 commented 1 year ago

Very interesting. I am going to try that.

Then do you would like that I add some link in the doc?

adamjseitz commented 1 year ago

Do you mean that the README should suggest to using gtirb-pprinter in the "Usage" section? - I think that would be a good idea. Is there somewhere else you think some improvement to the documentation would be helpful?

As far as directly contributing changes to ddisasm, note that we require outside contributors to sign our Contributor License Agreement - you can find that here: https://github.com/GrammaTech/ddisasm/blob/main/CONTRIBUTING.md#contributor-license-agreement

gogo2464 commented 1 year ago

It works perfectly for me. I did:

sudo apt install selinux-utils -y libselinux1-dev ;
gcc -nostartfiles ls.s  -o ls-out -l selinux
./ls-out

Today I will check the user agreement. Then I will make a PR for the documentation.

gogo2464 commented 1 year ago

I m almost ready to push documentation. Just need to check if I should not keep some how-to for me in the sake of my blog.

aeflores commented 1 year ago

I believe this is solved now.