title: How to call function (shm_open) in librt inside the glibc source code (malloc/morecore.c)?
body: <p>I am trying to modify the <code>__default_morecore</code> function in <code>malloc/morecore.c</code>. The original <code>__default_morecore</code> is a simple wrapper for <code>sbrk</code>, but I want to use <code>shm_open</code> function inside <code>__default_morecore</code> to create shared memory object. Here is the <code>malloc/morecore.c</code> code I modified:</p>
  <pre><code>...
  /* Include header files for shm_open */
  
  void *
  __default_morecore (ptrdiff_t increment)
  {
    int shm_fd;
    /* Create the shared memory object */
    shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0644);
    if (shm_fd &lt; 0){
      return NULL;
    }
  ...
  }
  </code></pre>
  <p>I am pretty sure that my codes are right since I tested the code snippet in a small program and it works. After making this change, it will show below error message when using <code>make</code> for glibc 2.32 sources:</p>
  <blockquote>
  <p>.../build/libc_pic.os: In function '__GI___default_morecore': <br/>
  .../malloc/morecore.c:69: undefined reference to `shm_open' <br/>
  collect2: error: ld returned 1 exit status <br/>
  ../Makerules:698: recipe for targe '.../build/libc.so' failed</p>
  </blockquote>
  <p>I searched online for this error, and it's typically becasue doesn't link with <code>-lrt</code>. I tried add <code>-lrt</code> in 'LDFLAGS' and 'LIBS' during <code>../configure</code>, but not working. Then I added <code>-lrt</code> at the end of <code>build-shlib</code> of <code>Makerules</code> (which is used to build <code>libc.so</code>), and the error was changed to be following:</p>
  <blockquote>
  <p>//lib/x86_64-linux-gnu/librt.so.1: undefined reference to `__clock_getcpuclockid@GLIBC_PRIVATE'</p>
  <p>//lib/x86_64-linux-gnu/librt.so.1: undefined reference to `__clock_nanosleep@GLIBC_PRIVATE'</p>
  <p>//lib/x86_64-linux-gnu/librt.so.1: undefined reference to `__clock_settime@GLIBC_PRIVATE'</p>
  <p>//lib/x86_64-linux-gnu/librt.so.1: undefined reference to `__clock_getres@GLIBC_PRIVATE'</p>
  <p>//lib/x86_64-linux-gnu/libpthread.so.0: undefined reference to `__libc_vfork@GLIBC_PRIVATE' <br/>
  collect2: error: ld returned 1 exit status
  ../Rules:215: recipe for target '/home/yifei/FSL_Repos/test_shm_mmap_malloc/glibc-malloc-modified/build/iconv/iconvconfig' failed</p>
  </blockquote>
  <p>The new error shows that multiple symbols from <code>librt.so</code> and <code>libpthread.so</code> are undefined. I tried add <code>-lpthread -lrt</code> to many places in glibc <code>Makefile/Makeconfig/Makerules</code>, but it doesn't work. Also, from the above errors, I found the linking libraries are from my system in-built libraries, not the path of current modified glibc which I am building. Is that an expected behavior? I think it should use the <code>librt.so</code> and <code>libpthread.so</code> in my modified glibc I am building, but I cannot find <code>librt.so</code>, <code>librt.a</code> and <code>libpthread.so</code> in my <code>build</code> folder after the <code>make</code> terminates. Does it mean these are not generated at the stage of the error I occur? If yes, it might be a dependency issue considering I called a glibc function inside glibc. <strong>How to resolve that issue?</strong></p>
  <p><strong>How can I call <code>shm_open</code> in <code>malloc/morecore.c</code> and link that librt without any error?</strong> Thanks!</p>