共享库代码中找到ELF aux头(或envp)的编程方式? [英] programatic way to find ELF aux header (or envp) in shared library code?

查看:286
本文介绍了共享库代码中找到ELF aux头(或envp)的编程方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种编程方式来找到 powerpc cpu类型在Linux上。执行一些google搜索会找到一个答案,提示我建议使用mfpvr指令,该指令在 ELF AUX标头,并且肯定可以使用以下命令获取正在运行的计算机的POWER5字符串:

I'm looking for a programatic way to find the powerpc cpu type on Linux. Performing some google searches associated an answer suggesting the mfpvr instruction I found that this is available in the ELF AUX header, and sure enough I can obtain the POWER5 string for the machine I'm running on with the following:

#include <stdio.h>
#include <elf.h>

int main( int argc, char **argv, char **envp )
{
   /* walk past all env pointers */
   while ( *envp++ != NULL )
      ;

   /* and find ELF auxiliary vectors (if this was an ELF binary) */
#if 0
   Elf32_auxv_t * auxv = (Elf32_auxv_t *) envp ;
#else
   Elf64_auxv_t * auxv = (Elf64_auxv_t *) envp ;
#endif

   char * platform = NULL ;

   for ( ; auxv->a_type != AT_NULL ; auxv++ )
   {
      if ( auxv->a_type == AT_PLATFORM )
      {
         platform = (char *)auxv->a_un.a_val ;
         break;
      }
   }

   if ( platform )
   {
      printf( "%s\n", platform ) ;
   }

   return 0 ;
}

在要使用此信息的共享库上下文中,我无权访问封装是否有其他编程方法可以找到ELF AUX标头的开头?

In the shared library context where I want to use this info I have no access to envp. Is there an alternate programatic method to find the beginning of the ELF AUX header?

推荐答案

如果从 / proc / self / auxv 文件

根据 man proc / proc / self / auxv 自内核版本2.6.0-test7起可用。

According to man proc /proc/self/auxv is available since kernel level 2.6.0-test7.

另一种选择-获取一些(现有)环境变量-假设 HOME
PATH 或其他。请注意,您将获得它的地址。从这里您可以返回并找到先前的env变量,然后在其之前找到一个,依此类推。之后,您同样可以跳过所有argv参数。然后您到达最后一个AUXV条目。退后一步-您应该可以找到您的 AT_PLATFORM

Another option - get some (existing) environment variable - let say HOME, or PATH, or whatever. Please note that you'll get it's ADDRESS. From here you can go back and find previous env variable, then one before it, etc. After that you can likewise skip all argv arguments. And then you get to the last AUXV entry. Some steps back - and you should be able find your AT_PLATFORM.

编辑:看来glibc现在提供了获取此信息的编程方法:

It looks like glibc now provides a programatic method to get at this info:

glibc-headers-2.17-106:/usr/include/sys/auxv.h:getauxinfo()

glibc-headers-2.17-106: /usr/include/sys/auxv.h : getauxinfo()

示例:

#include <sys/auxv.h>
#include <stdio.h>

int main()
{
   unsigned long v = getauxval( AT_PLATFORM ) ;
   printf( "%s\n", (char *)v ) ;

   return 0 ;
}

这篇关于共享库代码中找到ELF aux头(或envp)的编程方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆