【问题标题】:open syscall mode argument number confusion打开系统调用模式参数编号混淆
【发布时间】:2021-07-30 20:05:29
【问题描述】:

在x64,ubuntu 20机器上,我写了一个简单的C程序

#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);
} 

我试图了解 0770 文件模式参数生成的十六进制值。所以我 objdump 二进制文件并得到以下内容:

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>

很明显,0x1f8 是模式参数。但是,它对应的是十进制的 504。

0770 是如何转换为 504(或十六进制的 0x1f8)的?

【问题讨论】:

  • 0770 是八进制文字(以 8 为基数),相当于十进制的 504。

标签: c linux file system-calls


【解决方案1】:

0770是怎么转成504的

就像转换任何其他八进制数一样,将每个数字乘以其对应的 8 次幂:

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

【讨论】:

    猜你喜欢
    • 2012-01-20
    • 2016-04-16
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多