uClibc 분석

KiJeong·2021년 11월 22일
0

Linux

목록 보기
1/8

uClibc

임베디드 리눅스 전용으로 만들어진 소형 C 라이브러리이다.

glibc 와 uClibc

GLIBC 는 GNU C Library 를 뜻합니다.
C언어 자체는 input/ouput , 메모리관리, 문자열 조작 등의 기능이 없습니다.
stdio.h 헤더 파일을 include 해야
printf 함수를 가지고 출력을 하고
scanf 함수를 가지고 입력을 받을 수 있습니다.
malloc , free 함수가 있어야 메모리 관리를 하고
strcmp, strcat 등의 함수가 있어야 문자열을 조작할 수가 있습니다.
이러한 것들을 할 수 있도록 도와주는 C library 중에
GNU 에서 제공하는 GLIBC 가 있는 것 입니다.
uClibc 는 작업환경이 일반 PC 보다 비교적 협소한 임베디드 환경에서 사용할 수 있도록
GLIBC 를 축소한 형태의 library 입니다.
출처: https://tksssch29.tistory.com/

Building Embedded Linux Systems, 2nd Edition (2008).pdf 책에서 보았던 것들.

the various library files in a minimal glibc take up as much as 2 MB of storage space.

Given the constraints and limitations of embedded systems, the size of the standard GNU C library makes it an unlikely candidate for use on our target.
-> 임베디드 환경에서는 GNU C library 를 사용하는게 적절하지 않을수 있다.

When storage space on your target is limited, we recommend you use uClibc instead of glibc.
-> glibc 는 성능 향상 위주, uClibc는 메모리 사용량을 최소한으로 하게 함.

uClibc provides most of the same functionality. It is, of course, not as complete as the GNU library and does not attempt to comply with all the standards with which the GNU library complies.
-> GNU C 라이브러리에서 제공하는 것과 같은 것을 제공하지만, 모든 표준을 완벽하게 따르지는 않는다.

Executables built with alternative C libraries can be as little as one half the size as those built with glibc.
-> uClibc 를 사용하여 빌드한 실행파일의 크기는 glibc로 빌드한 것에 절반이다.

Keep in mind that almost every application is compiled against the C library headers. So, the C library also affects the size of application executable files and other libraries.
-> C library가 output 실행파일과 library의 크기에 큰 영향을 주는구나.

uClibc implements only ld, libc, libcrypt, libdl, libm, libpthread, libresolv, and libutil.
-> uClibc는 다음 라이브러리에 대해서만 대체한다.
-> 어쩐지 ld, libc, libcrypt, libdl, libm, libpthread 관련 에러는 한번씩 나왔었음.

Library component

  • ld: Dynamic linker
  • libc: Main C library routines
  • libcrypt: Cryptography routines
  • libdl: Routines for loading shared objects dynamically
  • libm: Math routines
  • libpthread: POSIX 1003.1c threads routines for Linux
  • libresolv: Name resolver routines
  • libutil: Login routines, part of the user accounting database

uClibc 사용 사례

Alpine Linux 는 주로 "보안, 단순성 및 자원 효율성을 높이는 고급 사용자"용으로 설계된 musl 및 BusyBox를 기반으로 한 Linux 배포판 입니다.
Alpine Linux는 이전에 가장 일반적으로 사용되는 전통적인 GNU C 라이브러리 (glibc) 대신 uClibc 를 사용했습니다. 경량이지만 glibc와 호환되지 않는 바이너리 의 중요한 결점이 있습니다. 따라서 uClibc와 함께 사용하기 위해 모든 소프트웨어를 컴파일해야 제대로 작동합니다. 2014 년 4 월 9 일부터 Alpine Linux 는 glibc와 부분적으로 호환되는 musl 로 전환되었습니다 .

실패 case

  1. 기존에 gclib-based로 사용하던 서드파티 라이브러리들을 그대로 사용해도 컴파일 될 줄 알았다. 같은 라이브러리를 지원한다고 생각했기 때문이다.
    -> uClibc로 실행 모듈이 링크되도록 컴파일 되도록 적용해야 한다. 그래야 uClibc를 사용하는 target system에서 수행할 수 있다.
  2. lm, lc, lpthread를 못찾는 에러
    다음 에러코드 참고
/usr/local/sabre/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.2.0/ld: cannot find crt1.o: No such file or directory
/usr/local/sabre/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.2.0/ld: cannot find crti.o: No such file or directory
/usr/local/sabre/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.2.0/ld: cannot find -lpthread
/usr/local/sabre/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.2.0/ld: cannot find -lm
/usr/local/sabre/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.2.0/ld: cannot find -lc

찾는 위치를 보니 "/usr/local/sabre/bin”이다. gcc, g++이 해당 위치에 있는 것을 사용하기때문에 lm, lc, lpthread 라이브러리도 같은 위치를 찾는것으로 보인다. 따라서, 위치를 uClibc로 빌드된 라이브러리로 바라볼수있게 CMakelists.txt 파일에서 정확히 정의(correct invocation)하는게 필요했다.

set(CMAKE_REQUIRED_LIBRARIES -lstdc++ -lc)

crt1.o, crti.o, crtn.o는 glibc로 컴파일된 것이다. 그래서 에러남.

생각해보니 해당 툴체인은 glibc를 사용하게 yocto 빌드되었다는거네. 근데 타겟 보드는 uclibc를 사용하게 되어있는데 상관없는것인가?
툴체인 위치의 라이브러리를 하나도 사용하지 않으니까 헤더파일, lib파일들도 적었었구나.

그 답은 여기있는것 같다. (uclibc 공식 홈페이지: https://www.uclibc.org/toolchains.html)
To use uClibc, you need to have a toolchain. A toolchain consists of GNU binutils, the GNU Compiler Collection GCC, and uClibc.
-> uClibc를 사용하기 위해서는 GNU binutils와 GNU Compiler Collection GCC, uClibc가 필요하다.

0개의 댓글