2017-12-24 16 views
0

3 개의 함수가있는 작은 라이브러리를 c에서 만들려고합니다.dlfcn.h 라이브러리 함수를 사용할 때 ELF 헤더 오류가 잘못되었습니다.

mm_alloc.h : 위의 세 가지 기능의 내부

/* 
* mm_alloc.h 
* 
* A clone of the interface documented in "man 3 malloc". 
*/ 

#pragma once 

#include <stdlib.h> 

void *mm_malloc(size_t size); 
void *mm_realloc(void *ptr, size_t size); 
void mm_free(void *ptr); 

내 OS는 우분투 지금

mm_test.c

#include "assert.h" 
#include "dlfcn.h" 
#include "stdio.h" 
#include "stdlib.h" 

/* Function pointers to hw3 functions */ 
void* (*mm_malloc)(size_t); 
void* (*mm_realloc)(void*, size_t); 
void (*mm_free)(void*); 

void load_alloc_functions() { 
    void *handle = dlopen(".(Its path here)../mm_alloc.h", RTLD_NOW); 
    if (!handle) { 
     fprintf(stderr, "%s\n", dlerror()); 
     exit(1); 
    } 

    char* error; 
    mm_malloc = dlsym(handle, "mm_malloc"); 
    if ((error = dlerror()) != NULL) { 
     fprintf(stderr, "%s\n", dlerror()); 
     exit(1); 
    } 

    mm_realloc = dlsym(handle, "mm_realloc"); 
    if ((error = dlerror()) != NULL) { 
     fprintf(stderr, "%s\n", dlerror()); 
     exit(1); 
    } 

    mm_free = dlsym(handle, "mm_free"); 
    if ((error = dlerror()) != NULL) { 
     fprintf(stderr, "%s\n", dlerror()); 
     exit(1); 
    } 
} 

int main() { 
    load_alloc_functions(); 
} 

때문이다 비어 여기 내 코드입니다. 나는 TMP를 실행하면

gcc mm_test.c -o tmp -ldl 

, 그것은 "잘못된 ELF 헤더"를 제공합니다 : 여기에 내가 코드를 컴파일하는 방법입니다. 이 문제를 어떻게 해결할 수 있습니까?

+0

주제가 맞지 않지만 'mm_malloc'과 친구들은 16 바이트의 정렬 된 데이터를 처리합니다. 교체품이 동일한 맞춤 품질을 제공하는지 확인하십시오. 몇 가지 시스템이 16 바이트 경계에 정렬 된 힙 메모리를 제공하지 않기 때문에 언급합니다 (Solaris가 떠오른다). – jww

+0

나는 이것을 내 마음에 간직 할 것이다. – JollyRoger

답변

1

dlopen()은 C 헤더 파일이 아닌 공유 라이브러리 파일 (.so 개) 만로드 할 수 있습니다.

이러한 기능을 구현하고이를로드 할 공유 라이브러리로 컴파일해야합니다.

+0

나는 그것을 몰랐다. 고마워. – JollyRoger