summaryrefslogtreecommitdiffstats
path: root/libdwfl
diff options
context:
space:
mode:
authorTimm Bäder <tbaeder@redhat.com>2020-11-26 15:10:48 +0100
committerMark Wielaard <mark@klomp.org>2020-11-28 02:00:40 +0100
commitdaec00e631c37e82f3d8cb5816b2e8141383c9d9 (patch)
tree2e29923b6ca328b4623ca72ca2cb18c70e863d51 /libdwfl
parentsegment_report_module: Pull read_portion() into file scope (diff)
downloadelfutils-daec00e631c37e82f3d8cb5816b2e8141383c9d9.tar.gz
elfutils-daec00e631c37e82f3d8cb5816b2e8141383c9d9.tar.bz2
elfutils-daec00e631c37e82f3d8cb5816b2e8141383c9d9.tar.xz
segment_report_module: Inline consider_notes() into only caller
Get rid of a nested function this way. Signed-off-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'libdwfl')
-rw-r--r--libdwfl/ChangeLog5
-rw-r--r--libdwfl/dwfl_segment_report_module.c167
2 files changed, 89 insertions, 83 deletions
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index f66ff3f0..a5cffc49 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,5 +1,10 @@
12020-11-26 Timm Bäder <tbaeder@redhat.com> 12020-11-26 Timm Bäder <tbaeder@redhat.com>
2 2
3 * dwfl_segment_report_module.c (dwfl_segment_report_module):
4 Remove consider_notes function. Inline code for type == PT_NOTE.
5
62020-11-26 Timm Bäder <tbaeder@redhat.com>
7
3 * dwfl_segment_report_module.c (read_portion): New static function. 8 * dwfl_segment_report_module.c (read_portion): New static function.
4 (dwfl_segment_report_module): Remove read_portion function. 9 (dwfl_segment_report_module): Remove read_portion function.
5 Call static function with read_state, start and segment. 10 Call static function with read_state, start and segment.
diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index aa4a47c1..8d99e3bb 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -466,88 +466,6 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
466 build_id.len = 0; 466 build_id.len = 0;
467 build_id.vaddr =0; 467 build_id.vaddr =0;
468 468
469 /* Consider a PT_NOTE we've found in the image. */
470 inline void consider_notes (GElf_Addr vaddr, GElf_Xword filesz,
471 GElf_Xword align)
472 {
473 /* If we have already seen a build ID, we don't care any more. */
474 if (build_id.memory != NULL || filesz == 0)
475 return;
476
477 void *data;
478 size_t data_size;
479 if (read_portion (&read_state, &data, &data_size,
480 start, segment, vaddr, filesz))
481 return;
482
483 /* data_size will be zero if we got everything from the initial
484 buffer, otherwise it will be the size of the new buffer that
485 could be read. */
486 if (data_size != 0)
487 filesz = data_size;
488
489 assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
490
491 void *notes;
492 if (ei_data == MY_ELFDATA)
493 notes = data;
494 else
495 {
496 notes = malloc (filesz);
497 if (unlikely (notes == NULL))
498 return;
499 xlatefrom.d_type = xlateto.d_type = (align == 8
500 ? ELF_T_NHDR8 : ELF_T_NHDR);
501 xlatefrom.d_buf = (void *) data;
502 xlatefrom.d_size = filesz;
503 xlateto.d_buf = notes;
504 xlateto.d_size = filesz;
505 if (elf32_xlatetom (&xlateto, &xlatefrom,
506 ehdr.e32.e_ident[EI_DATA]) == NULL)
507 goto done;
508 }
509
510 const GElf_Nhdr *nh = notes;
511 size_t len = 0;
512 while (filesz > len + sizeof (*nh))
513 {
514 const void *note_name;
515 const void *note_desc;
516
517 len += sizeof (*nh);
518 note_name = notes + len;
519
520 len += nh->n_namesz;
521 len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
522 note_desc = notes + len;
523
524 if (unlikely (filesz < len + nh->n_descsz))
525 break;
526
527 if (nh->n_type == NT_GNU_BUILD_ID
528 && nh->n_descsz > 0
529 && nh->n_namesz == sizeof "GNU"
530 && !memcmp (note_name, "GNU", sizeof "GNU"))
531 {
532 build_id.vaddr = note_desc - (const void *) notes + vaddr;
533 build_id.len = nh->n_descsz;
534 build_id.memory = malloc (build_id.len);
535 if (likely (build_id.memory != NULL))
536 memcpy (build_id.memory, note_desc, build_id.len);
537 break;
538 }
539
540 len += nh->n_descsz;
541 len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
542 nh = (void *) notes + len;
543 }
544
545 done:
546 if (notes != data)
547 free (notes);
548 finish_portion (&read_state, &data, &data_size);
549 }
550
551 Elf32_Phdr *p32 = phdrsp; 469 Elf32_Phdr *p32 = phdrsp;
552 Elf64_Phdr *p64 = phdrsp; 470 Elf64_Phdr *p64 = phdrsp;
553 if ((ei_class == ELFCLASS32 471 if ((ei_class == ELFCLASS32
@@ -577,9 +495,92 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
577 } 495 }
578 else if (type == PT_NOTE) 496 else if (type == PT_NOTE)
579 { 497 {
498 /* If we have already seen a build ID, we don't care any more. */
499 if (build_id.memory != NULL || filesz == 0)
500 continue; /* Next header */
501
580 /* We calculate from the p_offset of the note segment, 502 /* We calculate from the p_offset of the note segment,
581 because we don't yet know the bias for its p_vaddr. */ 503 because we don't yet know the bias for its p_vaddr. */
582 consider_notes ( start + offset, filesz, align); 504 const size_t note_vaddr = start + offset;
505 void *data;
506 size_t data_size;
507 if (read_portion (&read_state, &data, &data_size,
508 start, segment, note_vaddr, filesz))
509 continue; /* Next header */
510
511 /* data_size will be zero if we got everything from the initial
512 buffer, otherwise it will be the size of the new buffer that
513 could be read. */
514 if (data_size != 0)
515 filesz = data_size;
516
517 assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
518
519 void *notes;
520 if (ei_data == MY_ELFDATA)
521 notes = data;
522 else
523 {
524 const unsigned int xencoding = ehdr.e32.e_ident[EI_DATA];
525
526 notes = malloc (filesz);
527 if (unlikely (notes == NULL))
528 continue; /* Next header */
529 xlatefrom.d_type = xlateto.d_type = (align == 8
530 ? ELF_T_NHDR8
531 : ELF_T_NHDR);
532 xlatefrom.d_buf = (void *) data;
533 xlatefrom.d_size = filesz;
534 xlateto.d_buf = notes;
535 xlateto.d_size = filesz;
536 if (elf32_xlatetom (&xlateto, &xlatefrom, xencoding) == NULL)
537 {
538 free (notes);
539 finish_portion (&read_state, &data, &data_size);
540 continue;
541 }
542 }
543
544 const GElf_Nhdr *nh = notes;
545 size_t len = 0;
546 while (filesz > len + sizeof (*nh))
547 {
548 const void *note_name;
549 const void *note_desc;
550
551 len += sizeof (*nh);
552 note_name = notes + len;
553
554 len += nh->n_namesz;
555 len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
556 note_desc = notes + len;
557
558 if (unlikely (filesz < len + nh->n_descsz))
559 break;
560
561 if (nh->n_type == NT_GNU_BUILD_ID
562 && nh->n_descsz > 0
563 && nh->n_namesz == sizeof "GNU"
564 && !memcmp (note_name, "GNU", sizeof "GNU"))
565 {
566 build_id.vaddr = (note_desc
567 - (const void *) notes
568 + note_vaddr);
569 build_id.len = nh->n_descsz;
570 build_id.memory = malloc (build_id.len);
571 if (likely (build_id.memory != NULL))
572 memcpy (build_id.memory, note_desc, build_id.len);
573 break;
574 }
575
576 len += nh->n_descsz;
577 len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
578 nh = (void *) notes + len;
579 }
580
581 if (notes != data)
582 free (notes);
583 finish_portion (&read_state, &data, &data_size);
583 } 584 }
584 else if (type == PT_LOAD) 585 else if (type == PT_LOAD)
585 { 586 {