drdot
drdot

Reputation: 3347

open syscall mode argument number confusion

On x64, ubuntu 20 machine, I wrote a simple C program

#include<stdio.h>
#include<fcntl.h>
int main()
{
    // assume that foo.txt is already created
    int fd1 = open("foo.txt", O_CREAT | O_RDONLY, 0770); 
    close(fd1);
      
    exit(0);
} 

I am trying to understand the hex value generated by the 0770 file mode argument. So I objdump the binary and got the below:

0000000000001189 <main>:                                                                                                                                                     
    1189:       f3 0f 1e fa             endbr64                                                                                                                              
    118d:       55                      push   rbp                                                                                                                           
    118e:       48 89 e5                mov    rbp,rsp                                                                                                                       
    1191:       48 83 ec 10             sub    rsp,0x10                                                                                                                      
    1195:       ba f8 01 00 00          mov    edx,0x1f8                                                                                                                     
    119a:       be 40 00 00 00          mov    esi,0x40                                                                                                                      
    119f:       48 8d 3d 5e 0e 00 00    lea    rdi,[rip+0xe5e]        # 2004 <_IO_stdin_used+0x4>                                                                            
    11a6:       b8 00 00 00 00          mov    eax,0x0                                                                                                                       
    11ab:       e8 d0 fe ff ff          call   1080 <open@plt>

It is clear that 0x1f8 is the mode argument. However, it corresponds to 504 in decimal.

How did 0770 convert to 504 (or 0x1f8 in hex)?

Upvotes: 1

Views: 199

Answers (1)

KamilCuk
KamilCuk

Reputation: 141000

How did 0770 convert to 504

Just like you would to convert any other octal number, multiply each digit by its corresponding power of 8:

0770(8) = 7 * 8^2 + 7 * 8^1 + 0 * 8^0 = 7*64 + 7*8 = 504(10)

Upvotes: 5

Related Questions