https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85927

            Bug ID: 85927
           Summary: ud2 instruction generated starting with gcc 8
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ndesaulniers at google dot com
  Target Milestone: ---

From: https://lkml.org/lkml/2018/5/25/636

It seems that:

__attribute__((naked))
unsigned long save_flags(void) {
  unsigned long flags;
  asm volatile("pushf; pop %0"
               : "=rm" (flags)
               :
               : "memory");
  return flags;
}

compiled with:

$ gcc-8 temp.c -c -fstack-protector-strong -O3

generates:

$ objdump -d temp.o

temp.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <save_flags>:
   0:   9c                      pushfq 
   1:   58                      pop    %rax
   2:   0f 0b                   ud2 

Note that `ud2` is an explicit invalid opcode:
https://x86.puri.sm/html/file_module_x86_id_318.html

>From playing around with this in godbolt, this seems to have regressed from
gcc-7, which generates:

0000000000000000 <save_flags>:
   0:   9c                      pushfq 
   1:   58                      pop    %rax
   2:   c3                      retq

(but also gcc-7 warns:
temp.c:10:1: warning: ‘naked’ attribute directive ignored [-Wattributes]
).

Reply via email to