libelf_read.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /****************************************************************************
  2. * binfmt/libelf/libelf_read.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdint.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <debug.h>
  29. #include <errno.h>
  30. #include <nuttx/fs/fs.h>
  31. #include <nuttx/binfmt/elf.h>
  32. /****************************************************************************
  33. * Pre-processor Definitions
  34. ****************************************************************************/
  35. #undef ELF_DUMP_READDATA /* Define to dump all file data read */
  36. /****************************************************************************
  37. * Private Constant Data
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Private Functions
  41. ****************************************************************************/
  42. /****************************************************************************
  43. * Name: elf_dumpreaddata
  44. ****************************************************************************/
  45. #if defined(ELF_DUMP_READDATA)
  46. static inline void elf_dumpreaddata(FAR char *buffer, int buflen)
  47. {
  48. FAR uint32_t *buf32 = (FAR uint32_t *)buffer;
  49. int i;
  50. int j;
  51. for (i = 0; i < buflen; i += 32)
  52. {
  53. syslog(LOG_DEBUG, "%04x:", i);
  54. for (j = 0; j < 32; j += sizeof(uint32_t))
  55. {
  56. syslog(LOG_DEBUG, " %08x", *buf32++);
  57. }
  58. syslog(LOG_DEBUG, "\n");
  59. }
  60. }
  61. #else
  62. # define elf_dumpreaddata(b,n)
  63. #endif
  64. /****************************************************************************
  65. * Public Functions
  66. ****************************************************************************/
  67. /****************************************************************************
  68. * Name: elf_read
  69. *
  70. * Description:
  71. * Read 'readsize' bytes from the object file at 'offset'. The data is
  72. * read into 'buffer.' If 'buffer' is part of the ELF address environment,
  73. * then the caller is responsible for assuring that that address
  74. * environment is in place before calling this function (i.e., that
  75. * elf_addrenv_select() has been called if CONFIG_ARCH_ADDRENV=y).
  76. *
  77. * Returned Value:
  78. * 0 (OK) is returned on success and a negated errno is returned on
  79. * failure.
  80. *
  81. ****************************************************************************/
  82. int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer,
  83. size_t readsize, off_t offset)
  84. {
  85. ssize_t nbytes; /* Number of bytes read */
  86. off_t rpos; /* Position returned by lseek */
  87. binfo("Read %ld bytes from offset %ld\n", (long)readsize, (long)offset);
  88. /* Loop until all of the requested data has been read. */
  89. while (readsize > 0)
  90. {
  91. /* Seek to the next read position */
  92. rpos = nx_seek(loadinfo->filfd, offset, SEEK_SET);
  93. if (rpos != offset)
  94. {
  95. berr("Failed to seek to position %lu: %d\n",
  96. (unsigned long)offset, (int)rpos);
  97. return rpos;
  98. }
  99. /* Read the file data at offset into the user buffer */
  100. nbytes = nx_read(loadinfo->filfd, buffer, readsize);
  101. if (nbytes < 0)
  102. {
  103. /* EINTR just means that we received a signal */
  104. if (nbytes != -EINTR)
  105. {
  106. berr("Read from offset %lu failed: %d\n",
  107. (unsigned long)offset, (int)nbytes);
  108. return nbytes;
  109. }
  110. }
  111. else if (nbytes == 0)
  112. {
  113. berr("Unexpected end of file\n");
  114. return -ENODATA;
  115. }
  116. else
  117. {
  118. readsize -= nbytes;
  119. buffer += nbytes;
  120. offset += nbytes;
  121. }
  122. }
  123. elf_dumpreaddata(buffer, readsize);
  124. return OK;
  125. }