The question is, "Is there an intuitive way to obtain/compile the NASM
code from the source file?" (e.g., *.c
to *.out
binary file through NASM
).
From my independent research (which I will include below), the answer I came to is "no", but I hoped to get more expert input.
As I cited a few StackOverflow posts in this question, many good links show how to obtain and compile, but they are either a bit old or for 32-bit (How to generate a nasm compilable assembly code from c source code on Linux?), and because my question is regarding specifically about the 64-bit NASM
, I thought is not a possible duplicate.
At the moment, I am working on a project which involves assembly rewriting. I have a prototype working for the GAS
assembly file.
However, I have heard from my colleagues that using the NASM
assembly file (which I didn't know before) is better. After searching, I became convinced that NASM
seems to be indeed a better assembly format if I were to consider multiple cases of my project (the second post gave me a good reason why I should try to pursue NASM
):
To give a more precise example, the steps that are required for my assembly reassembling of GAS
files are done without needing to do any extraneous modifications as the following steps:
gcc -save-temps -masm=intel main.c foo.c bar.c (this will generate a-*.s automatically)
python3 reassemble_asm.py --files=a-foo.s,a-main.s,a-bar.s
as *.s -o *.o
gcc *.o -o main.out
In contrast, for NASM
though, to obtain the NASM
in the first place, it needs to go through multiple processes as written here: Converting C to nasm assembly, and then afterward, it becomes even more tricky for 64-bit compilation as it seemingly requires numerous modification: Relocation error when compiling NASM code in 64-bit mode.
If I try to compile 64-bit of the example shown here: Converting C to nasm assembly,
// hello.c
#include <stdio.h>
int main()
{
printf( "Hello World \n" );
return 0;
}
gcc -fno-asynchronous-unwind-tables -fno-pie -no-pie -s -c -o hello.o hello.c
objconv -fnasm hello.o
nasm -f elf64 hello.asm # this needs to be elf64, not elf
gcc hello.o -o hello
/usr/bin/ld: hello.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
This will not work due to relocation issues, as I mentioned above. I tried doing many solutions I found (-fno-pie
, default rel
, etc...), but to no avail (this demo is not related to my question, just wanted to provide this example to make the question more complete).
From what I have gathered/searched, my gut feeling is that NASM
is not intuitive in terms of generating/modifying/compiling the assembly file, and I need to stick with GAS
. However, because of my lack of assembly language knowledge (as I didn't even know about the NASM
until just now), I wanted to ensure I exhausted all of my options before making such a choice.
Furthermore, maybe there is still a way for me to use NASM
that I have not thought of yet; hence I wanted to ask this question.
Thank you in advance.