Objdump SH4 / can't disassemble for architecture UNKNOWN

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by Newbie »

Hi everybody,

I am just starting try to work with objdump with ELF file compiled by my SH4 tool chain.
But when i try to simply disassemble a ELF file with the command below :
objdump -d a.elf
It outputs this :
a.elf: file format elf32-little

objdump: can't disassemble for architecture UNKNOWN
My GCC tool chain work fine with SH4 as it cross compile and link with no problem.

So how can i make objdump working with my SH4 ELF(it does not work with object file like *.o)?

By the way, i try the readelf command line tool and it seems to work as it displays no error message.

But readelf does not have the output i want ...

Thanks for help.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by BlueCrab »

Use "sh-elf-objump" not just "objdump". Plain objdump is most likely your system one, not the SH one.
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by Newbie »

Sorry for the late answer, thanks i found out the sh-elf-objdump and it works nicely.

I have take some time to understand more ELF file structure : sections, header, attributes and so ...

By the way, i am searching a mean of making the exact opposite of the Elf2Bin conversion.

I mean taking an unscrambled raw binary and obtain the ELF file.

For me ELF is more interesting as i am in a learning phase.

I have read some source code in KOS utilities section but Bin2c, Bin2o, Bincvn could not resolve my Bin2Elf need.

Is there a way to make it ?

Thanks.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by BlueCrab »

You really can't take an bin file and recreate the elf. The elf file contains all the symbols and those are lost in the conversion to bin.

You could probably rig up a manner to at least partially recreate the sections of the elf file, but that's not all that useful in itself.
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by Newbie »

Ok.

But i have a strange idea on mind (perhaps it's stupid idea).
Imagine we have an unscrambled bin, then using the bin2o script in KOS obtaining a .o file.
Is it possible to compile (or link it) it to retrieve the original ELF file ?
If yes, how ?

Thanks
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by BlueCrab »

No. The bin2o and bin2c programs just take the raw binary itself and copy it byte-for-byte into a file. Nothing more, nothing less.

Once the symbols and all have been stripped from the program, there's no way to get them back without knowing where they are in the file (which would require either a symbol table or the original elf file).
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by Newbie »

Well, I suspected this response but it costs nothing to try :)

To complete this topic around ELF and Bin, I found in KOS two functions to execute a binary after loading it on on RAM :

Code: Select all


/** \brief  Replace the currently running binary.
    This function will replace the currently running binary with whatever is
    at the specified address. This function does not return.
    \param  image           The binary to run (already loaded into RAM).
    \param  length          The length of the binary.
    \param  address         The address of the binary's starting point.
*/
void arch_exec_at(const void *image, uint32 length, uint32 address) __noreturn;

/** \brief  Replace the currently running binary at the default address.
    This is a convenience function for arch_exec_at() that assumes that the
    binary has been set up with its starting point at the standard location.
    In the case of the Dreamcast, this is 0xAC010000 (or 0x8C010000, in P1).
    \param  image           The binary to run (already loaded into RAM).
    \param  length          The length of the binary.
*/
void arch_exec(const void *image, uint32 length) __noreturn;

I found an example in the examples directory :

Code: Select all


#include <kos.h>
#include <assert.h>

extern uint8 romdisk[];
KOS_INIT_ROMDISK(romdisk);

#define false (1 == 0)

int main(int argc, char **argv) 
{
    file_t f;
    void *subelf;

    /* Print a hello */
    printf("\n\nHello world from the exec.elf process\n");

    /* Map the sub-elf */
    f = fs_open("/rd/sub.bin", O_RDONLY);
    assert(f);
    subelf = fs_mmap(f);
    assert(subelf);

    /* Tell exec to replace us */
    printf("sub.bin mapped at %08x, jumping to it!\n\n\n", subelf);
    arch_exec(subelf, fs_total(f));

    /* Shouldn't get here */
    assert_msg(false, "exec call failed");

    return 0;
}

Could we have a different behavior in the sense that instead of killing the calling program by replacing it by the one called, we simply return to the calling program after the called program has ended ? I hope i am clear :)

If it is not implemented in some API in KOS, is there a way to make it (by ourselves) and it is possible to call raw binary instead of "canonical" ELF ?

Thanks for your help
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by BlueCrab »

arch_exec() and arch_exec_at() never return to the calling function except if there's an error (hence the __noreturn attribute). In theory, it would be possible to do so, but only if the called program is linked at a non-standard start address and you were exceedingly careful to not overwrite anything in the calling program -- which would be exceedingly difficult to do for most things.

KOS does support the idea of dynamically loadable libraries, which would be closer to what you want to do (and could in fact implement exactly the functionality that I think you're looking for). You can load a "library" and then search for a given function in it, then run it just like you were calling on any function pointer. Look here for some documentation on that functionality.
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by Newbie »

From what I understood creating a static library is just compiling all the functions independently to obtain object file (*.o).

I recall the "how to" below to see if I have correctly understood the process ....

A first function on a lib

Code: Select all

	void FunctionA(int *i)
	{
	   *i=5;
	}
A second function in the lib

Code: Select all

	void FunctionB(int *i)
	{
	   *i=5;
	}
Obtaining the object files

Code: Select all


 cc -Wall -c FunctionA.c FunctionB.c

Then gather them in Functions.a file with something like

Code: Select all


ar -cvq Functions.a FunctionA.o FunctionB.o

Now if we use the lib in a code like

Code: Select all


void FunctionA(int *);
void FunctionB(int *);

int main()
{
   int x;
   
   FunctionA(&x);
   
   return 0;
}

We must link to the lib like that

Code: Select all


cc -o executable-name prog.c Functions.a


In linux, the dynamic libraries are .so files.
It seems to be compiled by special arg with GCC

Something like :

Code: Select all


	gcc -Wall -fPIC -c *.c
	gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o 

Considering Linux OS like systems.

The opening of a shared library at runtime is made by "dlopen" and the closing by "dlclose" (http://man.yolinux.com/cgi-bin/man2html ... and=dlopen).

Code: Select all


#include <stdio.h>
#include <dlfcn.h>
#include "ctest.h"

int main(int argc, char **argv) 
{
   void *lib_handle;
   double (*fn)(int *);
   int x;
   char *error;

   lib_handle = dlopen("/opt/lib/libctest.so", RTLD_LAZY);
   if (!lib_handle) 
   {
      fprintf(stderr, "%s\n", dlerror());
      exit(1);
   }

   fn = dlsym(lib_handle, "ctest1");
   if ((error = dlerror()) != NULL)  
   {
      fprintf(stderr, "%s\n", error);
      exit(1);
   }

   (*fn)(&x);
   printf("Valx=%d\n",x);

   dlclose(lib_handle);
   return 0;
}

I imagine that there are not such functions available in KOS.

I think that dlopen is near to

Code: Select all

	klibrary_t * 	library_open (const char *name, const char *fn)
and dlclose near to

Code: Select all

	int 	library_close (klibrary_t *lib)
It could be a way of inspiration to solve my needs.

I see in the implementation these comment :
With KOS 1.3.x, we introduce the idea of a loadable library. It's a little
bit different from the standard *n*x concept of a library. Basically in
KOS, a library is a memory image plus a collection of exported names
associated with that image. Each library has a symbolic name and a version
that it can be referenced by, and all "shared libraries" are actually the
same exact library (there is no COW-style private storage per instance). So
it is somewhat similar to an AmigaOS style library or perhaps a Linux
kernel module.
By the way i see in the source code of "library_open" some things about the format of file :

Code: Select all

	// Use the ELF functions to load the memory image and extract the
    // entry points we need.
    if(elf_load(fn, lib, &lib->image) < 0) {
        library_destroy(lib);
        return NULL;
    }
So i ask myself how to generate a file that could be loaded from the "library_open" function ? is that a "canonical" ELF file ?

Finally, seeing the klibrary_t structure that "library_open" function returns, how to execute a function in a loaded library ?

It is a very interesting subject :)

Thanks for help !
Sangee
DCEmu Newbie
DCEmu Newbie
Posts: 1
Joined: Fri Sep 18, 2020 5:09 am
Has thanked: 0
Been thanked: 0

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by Sangee »

Too late reply may be,
I m trying to generate a assembly code from elf or binary files. Is it possible?
I tried with various commands but Im getting the following error -

riscv64-unknown-elf-objdump: can't disassemble for architecture UNKNOWN!

Do you know how to generate a assembly file from c file or object files?
mrneo240
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Mar 14, 2018 12:22 am
Has thanked: 16 times
Been thanked: 19 times

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by mrneo240 »

You can create elfs from dc bins.
Again it's really only useful for some tools that read elfs well and don't handle plain bins,
But it is possible. Note you aren't reconstructing the original elf but just one that resembles enough of the original to work in some tools.
TapamN
DC Developer
DC Developer
Posts: 104
Joined: Sun Oct 04, 2009 11:13 am
Has thanked: 2 times
Been thanked: 88 times

Re: Objdump SH4 / can't disassemble for architecture UNKNOWN

Post by TapamN »

If you specify the target object file as binary with the parameter "-b binary" you can disassemble raw binaries. You will also need to specify a machine type with "-m <MACHINE>". You can get a list of machine types with "[...]objdump -h".

As an example, to disassemble a Dreamcast 1ST_READ.BIN (unscrambled), use this command:

Code: Select all

sh-elf-objdump -D -b binary -m sh4 -EL --adjust-vma 0x8c010000 1ST_READ.BIN | less
"-EL" is little endian mode. "--adjust-vma" let's you control where the binary is loaded, so function pointers are correct and easily searchable.
Post Reply