Recent Posts

Pages: 1 [2] 3 4 ... 10
11
Programming with NASM / Re: Linux x86. Align segmtnt elf_i386 (p_align)
« Last post by fredericopissarra on April 27, 2023, 10:25:45 PM »
You didn't read a line about what I wrote about sections attributes, did ya?
12
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by AntonPotapov on April 27, 2023, 03:44:01 PM »
I found the types of sections: https://nasm.us/doc/nasmdoc8.html#section-8.9.2
Yep. Of those, usually you'll use .text, .data, .rodata and/or .bss.

All .xxx are system sections with predefined attributes. You can create your own sections without the '.', like in mysection which will have always the attributes noexec and nowrite, by default. Of course you can override this.

Since Linux (and Windows) use paging, these sections will be placed (probably) in different pages with specific attributes. Then, in your case, as you defined mysection without attributes, this page (4 KiB) will be readonly (noexec) and not executable (noexec), hence the segmentation fault. As I told you before, you only need to set exec attribute to override this:
Code: [Select]
  section mysection exec  ; mysection isn't a system section, now with exec attrib set.

[]s
Fred
Yes, thank you. Now I've got it figured out.
13
Programming with NASM / Linux x86. Align segmtnt elf_i386 (p_align)
« Last post by AntonPotapov on April 27, 2023, 03:31:58 PM »
ELF_i386 !!!
How do I align segments in the output elf file?
gcc aligns segments to 64 bytes
ld aligns to 4096 bytes.
I found this out by looking at the output program header (p_align)
https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
https://refspecs.linuxbase.org/elf/elf.pdf

I know that a good alignment improves the speed of the program. I don't know elf files that well. But how can I align segments on my own?

ld --help. I found parameters that remove the alignment:
  -n, --nmagic                Do not page align data
  -N, --omagic                Do not page align data, do not make text readonly

It also contains, but different elf formats (not elf_i386).
  --file-alignment <size>            Set file alignment
  --section-alignment <size>         Set section alignment

My usual linking
Code: [Select]
#/bin/bash
ld -m elf_i386 -o main main.o

remove alignment
Code: [Select]
#/bin/bash
ld -m elf_i386 -n -o main main.o
14
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by fredericopissarra on April 26, 2023, 06:36:32 PM »
I found the types of sections: https://nasm.us/doc/nasmdoc8.html#section-8.9.2
Yep. Of those, usually you'll use .text, .data, .rodata and/or .bss.

All .xxx are system sections with predefined attributes. You can create your own sections without the '.', like in mysection which will have always the attributes noexec and nowrite, by default. Of course you can override this.

Since Linux (and Windows) use paging, these sections will be placed (probably) in different pages with specific attributes. Then, in your case, as you defined mysection without attributes, this page (4 KiB) will be readonly (noexec) and not executable (noexec), hence the segmentation fault. As I told you before, you only need to set exec attribute to override this:
Code: [Select]
  section mysection exec  ; mysection isn't a system section, now with exec attrib set.

[]s
Fred
15
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by AntonPotapov on April 26, 2023, 02:26:49 PM »
16
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by AntonPotapov on April 26, 2023, 02:06:37 PM »
I recently started learning assembler. And I'm trying different ways of writing code. Next I plan to learn SSE. and above mentioned ABI, I'll read about it too.
Since you are using NASM (necessarily for x86), search for SysV ABI for i386 and amd64 (or x86_64) and study it... If you are using Windows, take a look at Microsoft calling conventions (or MS ABI)...

ABI are useful if you mix code with C and use syscalls/system libraries...
Thanks, I will keep that in mind. As long as I am programming in linux. It is easier for me to write programs in linux. or so it seems to me
17
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by fredericopissarra on April 26, 2023, 02:04:20 PM »
I recently started learning assembler. And I'm trying different ways of writing code. Next I plan to learn SSE. and above mentioned ABI, I'll read about it too.
Since you are using NASM (necessarily for x86), search for SysV ABI for i386 and amd64 (or x86_64) and study it... If you are using Windows, take a look at Microsoft calling conventions (or MS ABI)...

ABI are useful if you mix code with C and use syscalls/system libraries...
18
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by AntonPotapov on April 26, 2023, 02:01:14 PM »
Thanks to fredericopissarra
19
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by AntonPotapov on April 26, 2023, 01:59:25 PM »
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.

Best,
Frank
I recently started learning assembler. And I'm trying different ways of writing code. Next I plan to learn SSE. and above mentioned ABI, I'll read about it too.
20
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by fredericopissarra on April 25, 2023, 08:18:42 PM »
Frank was asking Anton why he is doing what he is doing :)
Oops! ;)
Pages: 1 [2] 3 4 ... 10