0

I have created two files - file.s

.data
.global hello
hello: .int 23

file1.s

.bss
.comm hello,32,4
.text
.global _start
_start:
mov $hello,%rbx
movl (%rbx),%eax
mov $60,%rax
xor %rdi,%rdi
syscall

now when linking, ld: warning: alignment 1 of symbol `hello' in file.o is smaller than 4 in file1.o I didn't get the alignment part. From the Doc ,

When using ELF or (as a GNU extension) PE, the .comm directive takes an optional third argument. This is the desired alignment of the symbol, specified for ELF as a byte boundary (for example, an alignment of 16 means that the least significant 4 bits of the address should be zero)

Now, why the least significant 4 bits should be zero? I assume because the 12 bits is the size of the symbol and the rest 4 bits now to be aligned to 16 is 0 ? Then why the warning is there from ld?

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Does this answer your question? [What is data alignment? Why and when should I be worried when typecasting pointers in C?](https://stackoverflow.com/questions/38875369/what-is-data-alignment-why-and-when-should-i-be-worried-when-typecasting-pointe) – phuclv Dec 22 '21 at 14:43
  • [Purpose of memory alignment](https://stackoverflow.com/q/381244/995714), [What is meant by "memory is 8 bytes aligned"?](https://stackoverflow.com/q/2846914/995714), [Why is memory alignment needed?](https://stackoverflow.com/q/46991418/995714) – phuclv Dec 22 '21 at 14:45
  • I understand the data alignment, what I am confused about is this fraction "the least significant 4 bits should be zero". Is my assumption correct for ELF file?And I did 4 bytes alignment (so that the hello that is defined on file.s could be aligned perfectly on this 32 bytes, if my assumption is correct). If my assumption is correct then why the warning? –  Dec 22 '21 at 14:52

1 Answers1

0

Pay attention to the error message: "alignment 1 of symbol 'hello' in file.o is smaller than 4". The problem is not with the .comm it's the .int in the first file that does not specify an alignment. To fix it, you can put a .balign 4 before the .int.

Jester
  • 56,577
  • 4
  • 81
  • 125