Android-- bionic介绍

Bionic简介

1、什么是Bionic?

Bionic是Android平台为C/C++开发者进行原生应用程序开发所提供的符合POSIX(可移植操作系统接口)标准的C库。

是Android操作系统BSD(UNIX操作系统中的一个分支)标准C库的衍生库。

Bionic提供了在Android平台上开发任何类型的功能性原生代码所需的最小构造集。

通俗地讲,Bionic(Bionic的头文件集)就是NDK的API

Bionic C库头文件在 %NDK_HOME%\platforms\android-<api-level>\arch-<architecture>\usr\include内。

2、其他的C标准库

只要是符合ANSI(美国国家标准学会) C标准的C库都可以叫做标准C库。

POSIX标准的C库是在ANSI标准的C库的基础上增加一些附加结构。

现有的著名的POSIX标准的C库有:GUN C库(glibc)和Embedded Linux C库(uClibc)。

注意,Bionic C库不以任何方式与其他标准C库二进制兼容,这意味着基于其他C库生成的二进制文件不应该与Bionic进行动态链接。

注意,虽然Android只提供Bionic C库,但是基于其他符合POSIX标准C库生成的应用,无可争议地可以在Android上面运行(因为Android本身是支持POSIX标准的)。

3、Bionic内容

Bionic提供了:C标准库宏、类型定义、函数,以及少数Android特有特性。

从功能角度Bionic提供了:

内存管理

文件输入/出

字符串操作

数学

日期和时间

进程控制

信号处理

网络套接字

多线程

用户和组

系统配置

命名服务切换

 

android bionic c 对比 gnu c

https://www.cnblogs.com/biglucky/p/6343794.html

Bionic 是一个BSD标准的C库,用在android平台上面的。

Android 是一个不完全开源的系统。 android的kernel使用的是基于linux的,linux使用的是GPL2的开源标准, 但是这种标准具备传染性,使用这个标注的软件不能收费。

android的设计是希望第三方的公司帮忙开发应用程序的。是需要商业话盈利的,所以为啦绕开GPL2标准,而使用略BSD的标准。因为这使得它们既可以使用开源软件,又不用向开源社区公开对软件所做的修改。

Bionic的优势:

  1.   由于删除头文件中许多的注释文件,代码段占用的空间非常少,早期的时候,bionic只要200Kb, 而gnu libc需要400kb.
  2.    BSD协议优势
  3.  可以运行在更低的CPU运转速度,大部分消费类电子都有待机的问题,和低功耗的模式,需要降频

Bionic限制:

  1. 不支持C++ exception
  2. 不支持C++ STL模板编程

Bionic Pthread:

  1.  不支持pthread_cancel
  2.  pthread_once 初始化的时候没有C++ 异常抛出
  3.  pthread_atfork()  

其他:

  1.  需要使用unicode编码
  2.  ABI bugs

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Android-- bionic介绍

https://blog.csdn.net/andyhuabing/article/details/7183369


小组人员移植ntfs-3g碰到lseek越界问题,经过查证其使用了llseek函数导致死机的问题。

其实问题很简单:
调用处: llseek (int, unsigned long, unsigned long, loff_t*, int);
但是在android bionic中将其对应到函数lseek,其函数声明如下:
off_t  lseek(int, off_t, int); 这个函数只支持32位的offset偏移,应该使用如下函数:
loff_t lseek64(int, loff_t, int); 这个函数支持64位的offset偏移


下面我们来简要介绍一下bionic的主要特性:
Bionic是Android的C/C++ library, libc是GNU/Linux以及其他类Unix系统的基础函数库,
最常用的就是GNU的libc,也叫glibc。Android之所以采用bionic而不是glibc,有几个原因:


1、版权问题,因为glibc是LGPL
2、库的体积和速度,bionic要比glibc小很多。
3、提供了一些Android特定的函数,getprop LOGI等


还有些也常用uclibc,这些库未使用的原因基本相似。


懒的写,从网上搜到的的贴一下bionic主要目录结构及主要功能:
|-- Android.mk

|-- CleanSpec.mk
|-- libc   (c 库)
|   |-- Android.mk
|   |-- arch-arm  (arm构架相关的实现,主要是针对arm的优化,以及和处理器相关的调用)
|   |-- arch-sh   (ST公司的SH4体系实现)
|   |-- arch-x86  (x86架构相关的实现)
|   |-- arch-mips (mips架构相关的实现)
|   |-- bionic
|   |-- CAVEATS
|   |-- docs
|   |-- include
|   |-- inet
|   |-- Jamfile
|   |-- kernel
|   |-- MODULE_LICENSE_BSD
|   |-- netbsd
|   |-- NOTICE
|   |-- private
|   |-- README
|   |-- regex
|   |-- stdio
|   |-- stdlib
|   |-- string
|   |-- SYSCALLS.TXT
|   |-- tools
|   |-- tzcode
|   |-- unistd
|   |-- wchar
|   `-- zoneinfo
|-- libdl       (动态链接库访问接口 dlopen dlsym dlerror dlclose dladdr的实现)
|   |-- Android.mk
|   |-- arch-sh
|   |-- dltest.c
|   |-- libdl.c
|   |-- MODULE_LICENSE_BSD
|   `-- NOTICE
|-- libm   (C数学函数库, 提供了常见的数序函数和浮点运算)
|   |-- alpha
|   |-- amd64
|   |-- Android.mk
|   |-- arm
|   |-- bsdsrc
|   |-- fpclassify.c
|   |-- i386
|   |-- i387
|   |-- ia64
|   |-- include
|   |-- isinf.c
|   |-- Makefile-orig
|   |-- man
|   |-- MODULE_LICENSE_BSD_LIKE
|   |-- NOTICE
|   |-- powerpc
|   |-- sh
|   |-- sincos.c
|   |-- sparc64
|   `-- src
|-- libstdc++  (standard c++ lib)
|   |-- Android.mk
|   |-- include
|   |-- MODULE_LICENSE_BSD
|   |-- NOTICE
|   `-- src
|-- libthread_db (线程调试库,可以利用此库对多线程程序进行调试)
|   |-- Android.mk
|   |-- include
|   |-- libthread_db.c
|   |-- MODULE_LICENSE_BSD
|   `-- NOTICE
|-- linker (Android dynamic linker)
|   |-- Android.mk
|   |-- arch
|   |-- ba.c
|   |-- ba.h
|   |-- debugger.c
|   |-- dlfcn.c
|   |-- linker.c
|   |-- linker_debug.h
|   |-- linker_format.c
|   |-- linker_format.h
|   |-- linker.h
|   |-- MODULE_LICENSE_APACHE2
|   |-- NOTICE
|   |-- README.TXT
|   `-- rt.c
|-- MAINTAINERS


下面就几个重要的观点说明一下:


Core Philosophy:


  The core idea behind Bionic's design is: KEEP IT REALLY SIMPLE.


  This implies that the C library should only provide lightweight wrappers
  around kernel facilities and not try to be too smart to deal with edge cases.


  The name "Bionic" comes from the fact that it is part-BSD and part-Linux:
  its source code consists in a mix of BSD C library pieces with custom
  Linux-specific bits used to deal with threads, processes, signals and a few
  others things.


  All original BSD pieces carry the BSD copyright disclaimer. Bionic-specific 
  bits carry the Android Open Source Project copyright disclaimer. And
  everything is released under the BSD license.
  
这些在overview.txt中说明的,不翻译了,英文简洁明了更好


1、系统调用sys_call问题
Syscall stubs:


  Each system call function is implemented by a tiny assembler source fragment
  (called a "syscall stub"), which is generated automatically by
  tools/gensyscalls.py which reads the SYSCALLS.TXT file for input.


基本上的系统调用函数上层进行简单的封装,然后利用汇编代码进行具体实现将利用int 80系统中断调
用到kernel内核中的函数,包含process management,file,signals,sockets,epoll等


举例说明下:
如下面的函数提供给外部使用即lseek64函数,进行简单的封装
loff_t lseek64(int fd, loff_t off, int whence)
{
   loff_t  result;

   if ( __llseek(fd, (unsigned long)(off >> 32),(unsigned long)(off), &result, whence ) < 0 )
       return -1;

   return result;
}

自动生成的汇编代码,主要调用__NR__xxx函数,进行内核中
/* autogenerated by gensyscalls.py */
#include <sys/linux-syscalls.h>

   .text
   .type __llseek, #function
   .globl __llseek
   .align 4
   .fnstart

__llseek:
   mov     ip, sp
   .save   {r4, r5, r6, r7}
   stmfd   sp!, {r4, r5, r6, r7}
   ldmfd   ip, {r4, r5, r6}
   ldr     r7, =__NR__llseek
   swi     #0
   ldmfd   sp!, {r4, r5, r6, r7}
   movs    r0, r0
   bxpl    lr
   b       __set_syscall_errno
   .fnend


这些代码都在bionic\libc\arch-$ARCh\syscalls下面


2、线程操作函数问题
   Bionic's C library comes with its own pthread implementation bundled in.
   This is different from other historical C libraries which:


    - place it in an external library (-lpthread)
    - play linker tricks with weak symbols at dynamic link time
    
    有如下的几个函数不支持:
    pthread_cancel()
    pthread_once()    

3、linux标准头文件问题 
Linux kernel headers:


  Bionic comes with its own set of "clean" Linux kernel headers to allow
  user-space code to use kernel-specific declarations (e.g. IOCTLs, structure
  declarations, constants, etc...). They are located in:


     ./kernel/common,
     ./kernel/arch-$ARCH


  These headers have been generated by a tool (kernel/tools/update-all.py) to
  only include the public definitions from the original Linux kernel headers.


4、外部引用bionic头文件
Include Paths:


  The Android build system should automatically provide the necessary include
  paths required to build against the C library headers. However, if you want
  to do that yourself, you will need to add:


      libc/arch-$ARCH/include
      libc/include
      libc/kernel/common
      libc/kernel/arch-$ARCH


  to your C include path.

 

这些就是android系统中使用的标C头文件所在处,请关注之


在这里面很多脚本使用python编写的,没有细读过.

 

猜你喜欢

转载自blog.csdn.net/u014426028/article/details/112613232