Detect target program Elf bit is 32 or 64

Source: Internet
Author: User

Android OS added support for 64-bit programs after 5.0, compatible with running 32-bit processes

The majority of the Android process is zygote the parent process fork out of the child process

The process that the zygote process is fork out is a 32-bit process

The process that the zygote64 process is fork out is a 64-bit process

But there are some processes before zygote start, that is, the init process fork, all belong to the 64bit process

Zygote process changes the name of the process after it has been forked (Setnicename)

If you want to find the process file based on the process name, the ELF bit of the target process is determined by reading the ELF header.

Not too ideal.

Through the man proc consult the document can know, parse the target process of AUXV file can judge Elf bit

Kernel support starting from 2.6, Android kernel version after this, detect AUXV file to judge Elf bit is reliable

First Makefile,application.mk.

App_abi: = arm64-v8a app_platform:= android-

Only compile 64-bit version, if the system cannot run 64-bit program, prove that the whole system is 32-bit

Android.mk

# Copyright (C) thethe Android Open Source project## Licensed under the Apache License, Version2.0(The"License"); ThisFile exceptinchcompliance with the license.# obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0# # unless required by applicable law or agreed toinchwriting, software# distributed under the License isDistributed on an" as is"basis,# without warranties or CONDITIONS of any KIND, either express or implied.# see the License forThe specific language governing permissions and# limitations under the License. #LOCAL_PATH:= $ (Call my-dir) include $ (clear_vars) Local_module:=Auxvlocal_src_files:=Auxv.clocal_arm_mode:=Arminclude $ (build_executable)

SOURCE Auxv.c

#include <stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>/** Parsing auxv:if you to parse the AUXV structure yourself (not relying on the dynamic loader) and then there ' s * a bit O f a conundrum:the AUXV structure follows the rule of the process it describes, so sizeof (unsigned * long) would be 4 for 32-bit processes and 8 for 64-bit processes. We can make a for us. In * order for this to work on 32-bit systems, all key codes must is 0xFFFFFFFF or less. On a 64-bit system, * The most significant-bits would be zero. Intel machines is little Endians, so these + bits follow * the least significant ones in memory. * As such, all your need to does is: * 1. Read bytes from the ' auxv ' file. * 2. Is this the end of the file?   * 3. Then it ' s a 64-bit process.   * 4. Done. * 5. Is buf[4], buf[5], buf[6] or buf[7] Non-zero?   * 6. Then it ' s a 32-bit process.   * 7. Done. * 8. Go to 1. */intCHECK_AUXV (intpid) {    if(PID <0) {printf ("Invalid process id\n"); return-1; }    Charauxv[ the]; snprintf (AUXV, the,"/PROC/%D/AUXV", PID); intFD =Open (AUXV, o_rdonly); if(FD <0) {printf ("%s does not exist\nprocess%d maybe running in + bit elf mode", AUXV, PID); return 0; }    Const intSIZE = -; CharBuf[size];  Do {        intNread =Read (FD, buf, SIZE); if(Nread <SIZE) {printf ("process%d running in a bit elf mode\n", PID);  Break; }                if(buf[4] && buf[5] && buf[6]) {printf ("process%d running in + bit elf mode @ line%d\n", PID, __line__); printf ("%x,%x,%x,%x\n", buf[4], buf[5], buf[6], buf[7]);  Break; }        Else if(buf[7]) {printf ("process%d running in + bit elf mode\n", PID);  Break; }    }  while(1);    Close (FD); return 0;}intMainintargcChar*argv[]) {    if(ARGC <=1) {printf ("USAGE:AUXV pid1 [Pid2 pid3 ...] \ n"); return 0; }    inti =0;  for(i =1; i < argc; ++i) {intPID =atoi (Argv[i]);    CHECK_AUXV (PID); }        return 0;}

Compile command

 call Ndk-build ndk_project_path=. Ndk_application_mk=./application.mk

Directory structure

|---application.mk|---build.bat|---jni| | ---android.mk| | ---auxv.c

Detect target program Elf bit is 32 or 64

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.