0

I have the following code with an inline assembly in C:

#include <stdlib.h>
#include <stdio.h>

__attribute__((noinline))
int *get_indecies(int *padding, int pad_size, int size, int alias_type);

int *get_indecies(int *padding, int pad_size, int size, int alias_type){
    int *indecies = (int *)malloc(size*sizeof(int));
    asm("dsb sy\n\t");
    return indecies;
}

At the bottom I the inline is inserted...

When I produce the object code using (cross compilation for aarch64) I have the inline inserted:

.....
 1f0:   d5033f9f        dsb     sy
 1f4:   aa1f03e1        mov     x1, xzr
......

when I link this binary with my main file using:

clang verification.c get_indecies.o -I "/home/[name]/gem5/include"  -L "/home/[name]/gem5/util/m5/build/arm64/out" -lm5 -lc -O0 -static -target aarch64-linux-gnu -o verfication-base-m5

and then I do an object dump of this to check if the assembly instruction is present using:

 aarch64-linux-gnu-objdump verification-base-m5 -S > assembly.s

The inline assembly does not exist.... Any ideas about what is happening in the linking stage that is removing this assembly instruction? The optimisation level is turned to 0 so I am not sure...

Thanks!

  • 1
    No the m5 stuff is not relevant to the problem - Sorry! Yes I also get the instruction when I do this ```clang -S get_indecies.c -target aarch64-linux-gnu, and checked get_indecies.s```. It is when I do the linking with the other file I do not get the instruction. (The instruction i do to link) ```clang verification.c get_indecies.o -I "/home/[name]/gem5/include" -L "/home/[name]/gem5/util/m5/build/arm64/out" -lm5 -lc -O0 -static -target aarch64-linux-gnu -o verfication-base-m5 ``` – Gandinator Sep 01 '22 at 16:33
  • 1
    Are you sure `verification.c` is calling `get_indecies`? Have you tried compiling `verification.c`, producing `verification.o`, and then creating the executable `verfication-base-m5` in another step? – Jardel Lucca Sep 01 '22 at 16:48
  • https://godbolt.org/z/6xM5cMEbW shows clang's asm output includes the instruction. But strangely, Godbolt's "binary" output mode that uses a disassembler doesn't show any output. That might be a compiler-explorer glitch. – Peter Cordes Sep 01 '22 at 18:17
  • That asm statement needs a memory clobber: `asm("dsb sy":::"memory")`, otherwise the compiler is permitted to move memory access past it, defeating the point of the memory barrier. – Timothy Baldwin Jan 09 '23 at 19:03

1 Answers1

0

I re-ran the below line recently:

clang verification.c get_indecies.o -I "/home/[name]/gem5/include"  -L "/home/[name]/gem5/util/m5/build/arm64/out" -lm5 -lc -O0 -static -target aarch64-linux-gnu -o verfication-base-m5

and then object dumped this and the inline assembly was present. Not sure what the bug was during the time but in my case it is resolved.