2

When I compile code with gcc - (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 - I find in the compiled code some unused code lines at the end of some/all functions or after some absolute jmp.

It seems the result is to align the code, but: Why don't use simply some 0s or NOPs?

The unused code lines I find are something such as the following code lines where I inserted <==:

  1. 134d:       eb b7                   jmp    1306 <msb32+0x16>
    134f:       90                      nop    <==
    1350:       b8 ff ff ff ff          mov    $0xffffffff,%eax
    1355:       c3                      retq
    1356:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1) <==
    135d:       00 00 00 
    
    0000000000001360 <object_key_compare_serials>:
    1360:       48 8b 07                mov    (%rdi),%rax
    
  2. 12fa:       74 44                   je     1340 <msb32+0x50>
    [...]
    1338:       c3                      retq
    1339:       0f 1f 80 00 00 00 00    nopl   0x0(%rax) <==
    1340:       c1 e7 10                shl    $0x10,%edi
    
phuclv
  • 37,963
  • 15
  • 156
  • 475
Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22
  • 2
    Do you notice anything about the addresses of the labels immediately following the `nop`s in question? – Jonathon Reinhart Jan 05 '20 at 21:28
  • @JonathonReinhart, line 1356 is never called and is at the end of the function. What do you mean when you say "Do you notice anything about ..."? – Sir Jo Black Jan 05 '20 at 21:31
  • 1
    @JonathonReinhart: Partially. My point is also WHY the use of different kind of instructions and not simply some NOPs code or some 0s. – Sir Jo Black Jan 05 '20 at 21:33
  • 1
    For the Why NOP question you would need to find the author of that code, which is better found through gnu than through stackoverflow. Examine the commit history and see if you can figure it out that way. – old_timer Jan 05 '20 at 21:39
  • 2
    See [this](https://softwareengineering.stackexchange.com/questions/158624/are-some-nop-codes-treated-differently-than-others) and [this](https://news.ycombinator.com/item?id=12369414). Short answer: Less effort is spent on decoding those instructions. – Jonathon Reinhart Jan 05 '20 at 21:51
  • https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf 3.5.1.9 – Sir Jo Black Jan 05 '20 at 22:16
  • I wonder if the CPU actually decodes those instructions at all, given that the preceding RET means they can never be executed. Is it smart enough to know that? – Nate Eldredge Jan 06 '20 at 00:41
  • @NateEldredge, The CPU certainly doesn't execute these instructions ( if the values in the stack used by `retq` don't point to them, but it shouldn't be! ). See also https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf paragraph 3.5.1.9. – Sir Jo Black Jan 06 '20 at 06:13

0 Answers0