1

我正在查看iPXE源代码,试图了解在启动的这个阶段早期会发生什么,但是有一些主要的嵌套宏扩展正在进行,我不熟悉并且难以理解。

在其中一个源文件 ( core/init.c) 中,有:

void initialise ( void ) {
    struct init_fn *init_fn;

    /* Call registered initialisation functions */
    for_each_table_entry ( init_fn, INIT_FNS )
            init_fn->initialise ();
}

所以我正在经历for_each_table_entry.

以下是一些相关的宏定义:

#define for_each_table_entry( pointer, table )                          \
    for ( pointer = table_start ( table ) ;                         \
          pointer < table_end ( table ) ;                           \
          pointer++ )

#define table_start( table ) __table_entries ( table, 00 )

#define table_end( table ) __table_entries ( table, 99 )

#define __table_entries( table, idx ) ( {                               \
    static __table_type ( table ) __table_entries[0]                \
            __table_entry ( table, idx )                            \
            __attribute__ (( unused ));                             \
    __table_entries; } )

#define __table_type( table ) __table_extract_type table
#define __table_extract_type( type, name ) type

#define __table_entry( table, idx )                                     \
    __attribute__ (( __section__ ( __table_section ( table, idx ) ),\
                     __aligned__ ( __table_alignment ( table ) ) ))

#define __table_section( table, idx ) \
    ".tbl." __table_name ( table ) "." __table_str ( idx )
#define __table_str( x ) #x

#define __table_alignment( table ) __alignof__ ( __table_type ( table ) )

#define __table_name( table ) __table_extract_name table
#define __table_extract_name( type, name ) name

呸,我想可能是这样。在我看来,嵌套扩展的数量非常可笑。

因此,如果我尝试手动扩展以查看发生了什么,我会得到:

# 1
for_each_table_entry ( init_fn, INIT_FNS )

# 2
for ( init_fn = table_start ( INIT_FNS ) ;                               \
      init_fn < table_end ( INIT_FNS ) ;                                 \
      init_fn++ )

# 3
for ( init_fn = __table_entries ( INIT_FNS, 00 ) ;                       \
      init_fn < __table_entries ( INIT_FNS, 00 ) ;                       \
      init_fn++ )

# 4
for ( init_fn = ( {                                                      \
    static __table_type ( INIT_FNS ) __table_entries[0]                  \
            __table_entry ( INIT_FNS, 00 )                               \
            __attribute__ (( unused ));                                  \
    __table_entries; } ) ;                                               \
      init_fn < ( {                                                      \
    static __table_type ( INIT_FNS ) __table_entries[0]                  \
            __table_entry ( INIT_FNS, 99 )                               \
            __attribute__ (( unused ));                                  \
    __table_entries; } ) ;                                               \
      init_fn++ )

# 5
for ( init_fn = ( {                                                           \
    static  __table_extract_type INIT_FNS __table_entries[0]                  \
            __attribute__ (( __section__ ( __table_section ( INIT_FNS, 00 ) ),\
                 __aligned__ ( __table_alignment ( INIT_FNS ) ) ))            \
            __attribute__ (( unused ));                                       \
    __table_entries; } ) ;                                                    \
      init_fn < ( {                                                           \
    static __table_extract_type INIT_FNS __table_entries[0]               
            __attribute__ (( __section__ ( __table_section ( INIT_FNS, 99 ) ),\
                 __aligned__ ( __table_alignment ( INIT_FNS ) ) ))
            __attribute__ (( unused ));                                       \
    __table_entries; } ) ;                                                    \
      init_fn++ )

# 6
for ( init_fn = ( {                                                           \
    static  __table_extract_type INIT_FNS __table_entries[0]                  \
            __attribute__ (( __section__ ( ".tbl." __table_name ( INIT_FNS ) "." __table_str ( 00 ) ),\
                 __aligned__ ( __alignof__ ( __table_type ( INIT_FNS ) ) ) ))            \
            __attribute__ (( unused ));                                       \
    __table_entries; } ) ;                                                    \
      init_fn < ( {                                                           \
    static __table_extract_type INIT_FNS __table_entries[0]               
            __attribute__ (( __section__ ( ".tbl." __table_name ( INIT_FNS ) "." __table_str ( 99 ),\
                 __aligned__ ( __alignof__ ( __table_type ( INIT_FNS ) ) ) ))
            __attribute__ (( unused ));                                       \
    __table_entries; } ) ;                                                    \
      init_fn++ )

# 7
for ( init_fn = ( {                                                           \
    static  __table_extract_type INIT_FNS __table_entries[0]                  \
            __attribute__ (( __section__ ( ".tbl." __table_extract_name INIT_FNS "." #00 ),\
                 __aligned__ ( __alignof__ ( __table_extract_type INIT_FNS ) ) ))            \
            __attribute__ (( unused ));                                       \
    __table_entries; } ) ;                                                    \
      init_fn < ( {                                                           \
    static __table_extract_type INIT_FNS __table_entries[0]               
            __attribute__ (( __section__ ( ".tbl." __table_extract_name INIT_FNS "." #99,\
                 __aligned__ ( __alignof__ ( __table_extract_type INIT_FNS ) ) ))
            __attribute__ (( unused ));                                       \
    __table_entries; } ) ;                                                    \
      init_fn++ )

地狱蝙蝠侠,我想就是这样。我的意思是,看起来那里有更多的宏,所以我可能搞砸了,但我没有看到任何对象类型的宏定义

  • __table_extract_type
  • __table_extract_name
  • __table_entries
  • __attribute__
  • __aligned__
    • 有一个定义。因为__aligned__attribute__,也许我搞砸了?
  • __section__
  • __alignof__

那么我的扩展是否正确?如果是这样,这甚至是有效的代码吗?这是低级 C/ASM 的东西吗?我知道组装的基础知识,但几年没有使用它,所以我不确定这里发生了什么。

4

0 回答 0