summaryrefstree
path: root/kernel/drivers/vision/dsp/dl/dsp-elf-loader.c
blob: 1241a26c014cbc2e5501455b1e2c4caf8741e858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
// SPDX-License-Identifier: GPL-2.0
/*
 * Samsung Exynos SoC series dsp driver
 *
 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com/
 */

#include <linux/kernel.h>
#include "dl/dsp-elf-loader.h"
#include "dl/dsp-lib-manager.h"
#include "dl/dsp-common.h"

static const char elf_magic[] = {
	0x7f,
	'E',
	'L',
	'F'
};

static const char elf_class[][6] = {
	"\0",
	"ELF32",
	"ELF64"
};

static const char elf_endian[][15] = {
	"\0",
	"Little endian",
	"Big endian"
};

static const char elf_os[][20] = {
	"System V",
	"HP-UX",
	"NetBSD",
	"Linux",
	"GNU Hurd",
	"Solaris",
	"AIX",
	"IRIX",
	"FreeBSD",
	"Tru64",
	"Novell Modesto",
	"OpenBSD",
	"OpenVMS",
	"NonStop Kernel",
	"AROS",
	"Fenix OS",
	"Cloud ABI"
};

static const char elf_type[][10] = {
	"NONE",
	"REL",
	"EXEC",
	"DYN",
	"CORE",
	"LOOS",
	"HIOS",
	"LOPROC",
	"HIPROC"
};

unsigned int dsp_elf32_rela_get_sym_idx(struct dsp_elf32_rela *rela)
{
	return rela->r_info >> 8;
}

unsigned int dsp_elf32_rela_get_rule_idx(struct dsp_elf32_rela *rela)
{
	return rela->r_info & ((1 << 8) - 1);
}

static void __dsp_elf32_init_mem(struct dsp_elf32_mem *mem)
{
	dsp_list_head_init(&mem->robss);
	dsp_list_head_init(&mem->rodata);
	dsp_list_head_init(&mem->bss);
	dsp_list_head_init(&mem->data);
	dsp_list_head_init(&mem->rela);
}

static void __dsp_elf32_init(struct dsp_elf32 *elf)
{
	elf->data = NULL;
	__dsp_elf32_init_mem(&elf->DMb);
	__dsp_elf32_init_mem(&elf->DMb_local);
	__dsp_elf32_init_mem(&elf->DRAMb);
	__dsp_elf32_init_mem(&elf->TCMb);
	__dsp_elf32_init_mem(&elf->TCMb_local);
	__dsp_elf32_init_mem(&elf->SFRw);
	dsp_list_head_init(&elf->text.text);
	dsp_list_head_init(&elf->text.rela);
	dsp_list_head_init(&elf->bss_sym);
	dsp_list_head_init(&elf->extern_sym);
	dsp_hash_tab_init(&elf->symhash);
}

static int __dsp_elf32_check_magic(struct dsp_dl_lib_file *file)
{
	int ck = 0;
	int idx;

	if (file->size < 4) {
		DL_ERROR("Elf file is invalid(%u)\n", file->size);
		return -1;
	}

	for (idx = 0; idx < 4; idx++)
		if (((char *)(file->mem))[idx] != elf_magic[idx])
			ck = -1;

	return ck;
}

static void *__dsp_elf32_get_ptr(struct dsp_elf32 *elf, void *src, size_t size)
{
	if ((src < (void *)elf->data) ||
			(src + size > (void *)elf->data + elf->size)) {
		DL_ERROR("invalid ptr access\n");
		return NULL;
	}

	if (src + size < src) {
		DL_ERROR("ptr overflow\n");
		return NULL;
	}

	return src;
}

static int __dsp_elf32_get_symtab(int idx, struct dsp_elf32 *elf)
{
	struct dsp_elf32_shdr *shdr;
	struct dsp_elf32_shdr *str_hdr;

	shdr = __dsp_elf32_get_ptr(elf, elf->shdr + idx,
			sizeof(struct dsp_elf32_shdr));
	if (!shdr) {
		DL_ERROR("shdr is NULL\n");
		return -1;
	}

	if (!shdr->sh_entsize) {
		DL_ERROR("sh_entsize must not be zero\n");
		return -1;
	}
	elf->symtab_num = shdr->sh_size / shdr->sh_entsize;

	elf->symtab = __dsp_elf32_get_ptr(elf, elf->data + shdr->sh_offset,
			sizeof(struct dsp_elf32_sym) * elf->symtab_num);
	if (!elf->symtab) {
		DL_ERROR("elf->symtab is NULL\n");
		return -1;
	}

	str_hdr = __dsp_elf32_get_ptr(elf, elf->shdr + shdr->sh_link,
			sizeof(struct dsp_elf32_shdr));
	if (!str_hdr) {
		DL_ERROR("str_hdr is NULL\n");
		return -1;
	}

	elf->strtab = __dsp_elf32_get_ptr(elf, elf->data + str_hdr->sh_offset,
			sizeof(char));
	if (!elf->strtab) {
		DL_ERROR("elf->strtab is NULL\n");
		return -1;
	}

	return 0;
}

static void __dsp_elf32_get_text(int idx, struct dsp_elf32 *elf)
{
	struct dsp_elf32_idx_node *sec;

	DL_DEBUG("Elf32 get text\n");
	sec = (struct dsp_elf32_idx_node *)dsp_dl_malloc(
			sizeof(*sec), "Text idx node");
	sec->idx = idx;
	dsp_list_node_init(&sec->node);
	dsp_list_node_push_back(&elf->text.text, &sec->node);
}

static void __dsp_elf32_idx_node_push_back(int idx, struct dsp_list_head *head)
{
	struct dsp_elf32_idx_node *sec;

	sec = (struct dsp_elf32_idx_node *)dsp_dl_malloc(
			sizeof(*sec), "Idx node");
	sec->idx = idx;
	dsp_list_node_init(&sec->node);
	dsp_list_node_push_back(head, &sec->node);
}

static void __dsp_elf32_get_mem(int idx, const char *mem_name,
	struct dsp_list_head *DMb, struct dsp_list_head *DMb_local,
	struct dsp_list_head *DRAMb, struct dsp_list_head *TCMb,
	struct dsp_list_head *TCMb_local, struct dsp_list_head *SFRw)
{
	DL_DEBUG("Elf32 get mem\n");

	if (strncmp("DMb", mem_name, 3) == 0) {
		const char *local_name = mem_name + 5;

		DL_DEBUG("local_name : %s(%zu)\n",
			local_name, strlen(local_name));

		if (strlen(local_name) == 13 &&
			strncmp(".thread_local", local_name, 13) == 0) {
			DL_DEBUG("sec : %d to DMb_local\n", idx);
			__dsp_elf32_idx_node_push_back(idx, DMb_local);
		} else {
			DL_DEBUG("sec : %d to DMb\n", idx);
			__dsp_elf32_idx_node_push_back(idx, DMb);
		}
	} else if (strncmp("TCMb", mem_name, 4) == 0) {
		const char *local_name = mem_name + 6;

		DL_DEBUG("local_name : %s(%zu)\n",
			local_name, strlen(local_name));

		if (strlen(local_name) == 13 &&
			strncmp(".thread_local", local_name, 13) == 0) {
			DL_DEBUG("sec : %d to TCMb_local\n", idx);
			__dsp_elf32_idx_node_push_back(idx, TCMb_local);
		} else {
			DL_DEBUG("sec : %d to TCMb\n", idx);
			__dsp_elf32_idx_node_push_back(idx, TCMb);
		}
	} else if (strncmp("DRAMb", mem_name, 5) == 0)
		__dsp_elf32_idx_node_push_back(idx, DRAMb);
	else if (strncmp("SFRw", mem_name, 4) == 0)
		__dsp_elf32_idx_node_push_back(idx, SFRw);
}

static int __dsp_elf32_rela_node_push_back(int idx, struct dsp_list_head *head,
	struct dsp_elf32 *elf)
{
	struct dsp_elf32_shdr *shdr;
	struct dsp_elf32_rela_node *sec;

	shdr = __dsp_elf32_get_ptr(elf, elf->shdr + idx,
			sizeof(struct dsp_elf32_shdr));
	if (!shdr) {
		DL_ERROR("shdr is NULL\n");
		return -1;
	}

	if (!shdr->sh_entsize) {
		DL_ERROR("sh_entsize must not be zero\n");
		return -1;
	}

	sec = (struct dsp_elf32_rela_node *)dsp_dl_malloc(sizeof(*sec),
			"Rela node");
	sec->idx = idx;
	sec->rela_num = shdr->sh_size / shdr->sh_entsize;
	sec->rela = __dsp_elf32_get_ptr(elf, elf->data + shdr->sh_offset,
			sizeof(struct dsp_elf32_rela) * sec->rela_num);
	if (!sec->rela) {
		DL_ERROR("sec->rela is NULL\n");
		return -1;
	}

	dsp_list_node_init(&sec->node);
	dsp_list_node_push_back(head, &sec->node);
	return 0;
}

static int __dsp_elf32_get_mem_rela(int idx, const char *sec_name,
	struct dsp_elf32 *elf, struct dsp_list_head *text,
	struct dsp_list_head *DMb, struct dsp_list_head *DMb_local,
	struct dsp_list_head *DRAMb, struct dsp_list_head *TCMb,
	struct dsp_list_head *TCMb_local, struct dsp_list_head *SFRw)
{
	int ret;
	struct dsp_list_head *head = NULL;

	DL_DEBUG("Elf32 get mem rela\n");

	if (strncmp("text", sec_name, 4) == 0) {
		head = text;
	} else if (strncmp("data.", sec_name, 5) == 0) {
		const char *mem_name = sec_name + 5;

		if (strncmp("DMb", mem_name, 3) == 0) {
			const char *local_name = mem_name + 5;

			if (strlen(local_name) == 13 &&
					strncmp(".thread_local", local_name,
						13) == 0)
				head = DMb_local;
			else
				head = DMb;
		} else if (strncmp("TCMb", mem_name, 4) == 0) {
			const char *local_name = mem_name + 6;

			if (strlen(local_name) == 13 &&
					strncmp(".thread_local", local_name,
						13) == 0)
				head = TCMb_local;
			else
				head = TCMb;
		} else if (strncmp("DRAMb", mem_name, 5) == 0) {
			head = DRAMb;
		} else if (strncmp("SFRw", mem_name, 4) == 0) {
			head = SFRw;
		}
	}

	if (head) {
		ret = __dsp_elf32_rela_node_push_back(idx, head, elf);
		if (ret == -1) {
			DL_ERROR("Failed rela node push back\n");
			return -1;
		}
	}
	return 0;
}

static int __dsp_elf32_get_bss_symhash(struct dsp_elf32 *elf)
{
	unsigned int idx;
	struct dsp_elf32_sym *sym;
	int ndx;
	struct dsp_elf32_idx_node *symnode;

	DL_DEBUG("Elf32 get bss symhash\n");

	for (idx = 0; idx < elf->symtab_num; idx++) {
		sym = &elf->symtab[idx];
		ndx = sym->st_shndx;

		if (ndx > 0 && ndx < elf->hdr->e_shnum) {
			const char *sym_str;
			const char *shstr;

			sym_str = __dsp_elf32_get_ptr(elf, elf->strtab +
					sym->st_name, sizeof(char));
			if (!sym_str) {
				DL_ERROR("sym_str is NULL\n");
				return -1;
			}

			shstr = __dsp_elf32_get_ptr(elf, elf->shstrtab +
					elf->shdr[ndx].sh_name, sizeof(char));
			if (!shstr) {
				DL_ERROR("shstr is NULL\n");
				return -1;
			}

			dsp_hash_push(&elf->symhash, sym_str, sym);

			if (strncmp(".bss", shstr, 4) == 0 ||
				strncmp(".robss", shstr, 6) == 0) {
				symnode =
					(struct dsp_elf32_idx_node *)
					dsp_dl_malloc(
						sizeof(*symnode),
						"bss sym Idx node");
				symnode->idx = idx;
				dsp_list_node_init(&symnode->node);
				dsp_list_node_push_back(&elf->bss_sym,
					&symnode->node);
			}
		}
	}

	return 0;
}

static int __dsp_elf32_get_extern_sym(struct dsp_elf32 *elf)
{
	int ret;
	unsigned int idx;
	struct dsp_elf32_sym *sym;
	int ndx;
	struct dsp_elf32_idx_node *symnode;

	DL_DEBUG("Elf32 get extern symbols\n");

	for (idx = 0; idx < elf->symtab_num; idx++) {
		sym = &elf->symtab[idx];
		ndx = sym->st_shndx;

		if (ndx == 0) {
			const char *sym_str;

			sym_str = __dsp_elf32_get_ptr(elf, elf->strtab +
					sym->st_name, sizeof(char));
			if (!sym_str) {
				DL_ERROR("sym_str is NULL\n");
				return -1;
			}

			ret = dsp_hash_get(&elf->symhash, sym_str,
					(void **)&sym);

			if (ret == -1) {
				symnode =
					(struct dsp_elf32_idx_node *)
					dsp_dl_malloc(
						sizeof(*symnode),
						"extern sym node");
				symnode->idx = idx;
				dsp_list_node_init(&symnode->node);
				dsp_list_node_push_back(&elf->extern_sym,
					&symnode->node);
			}
		}
	}

	return 0;
}

int dsp_elf32_load(struct dsp_elf32 *elf, struct dsp_dl_lib_file *file)
{
	int ret;
	struct dsp_elf32_shdr *shstrtab_hdr;
	int idx;

	DL_DEBUG("Elf32 load\n");

	ret = __dsp_elf32_check_magic(file);
	if (ret == -1) {
		DL_ERROR("CHK_ERR\n");
		return -1;
	}

	elf->data = (char *)file->mem;
	elf->size = file->size;

	elf->hdr = __dsp_elf32_get_ptr(elf, elf->data,
			sizeof(struct dsp_elf32_hdr));
	if (!elf->hdr) {
		DL_ERROR("elf->hdr is NULL\n");
		return -1;
	}

	elf->shdr = __dsp_elf32_get_ptr(elf, elf->data + elf->hdr->e_shoff,
			sizeof(struct dsp_elf32_shdr));
	if (!elf->shdr) {
		DL_ERROR("elf->shdr is NULL\n");
		return -1;
	}
	elf->shdr_num = elf->hdr->e_shnum;

	shstrtab_hdr = __dsp_elf32_get_ptr(elf, elf->shdr +
			elf->hdr->e_shstrndx, sizeof(struct dsp_elf32_shdr));
	if (!shstrtab_hdr) {
		DL_ERROR("shstrtab_hdr is NULL\n");
		return -1;
	}

	elf->shstrtab = __dsp_elf32_get_ptr(elf, elf->data +
			shstrtab_hdr->sh_offset, sizeof(char));
	if (!elf->shstrtab) {
		DL_ERROR("elf->shstrtab is NULL\n");
		return -1;
	}

	for (idx = 0; idx < elf->hdr->e_shnum; idx++) {
		struct dsp_elf32_shdr *shdr;
		const char *shdr_name;

		shdr = __dsp_elf32_get_ptr(elf, elf->shdr + idx,
				sizeof(struct dsp_elf32_shdr));
		if (!shdr) {
			DL_ERROR("shdr is NULL\n");
			return -1;
		}

		shdr_name = __dsp_elf32_get_ptr(elf, elf->shstrtab +
				shdr->sh_name, sizeof(char));
		if (!shdr_name) {
			DL_ERROR("shdr_name is NULL\n");
			return -1;
		}

		if (shdr->sh_type == 2) {
			ret = __dsp_elf32_get_symtab(idx, elf);
			if (ret == -1) {
				DL_ERROR("Failed to get symtab\n");
				return -1;
			}
		} else if (strcmp(".text", shdr_name) == 0) {
			__dsp_elf32_get_text(idx, elf);
		} else if (strncmp(".robss.", shdr_name, 7) == 0) {
			const char *mem_name = shdr_name + 7;

			__dsp_elf32_get_mem(idx, mem_name, &elf->DMb.robss,
				&elf->DMb_local.robss,
				&elf->DRAMb.robss, &elf->TCMb.robss,
				&elf->TCMb_local.robss,
				&elf->SFRw.robss);
		} else if (strncmp(".bss.", shdr_name, 5) == 0) {
			const char *mem_name = shdr_name + 5;

			__dsp_elf32_get_mem(idx, mem_name, &elf->DMb.bss,
				&elf->DMb_local.bss, &elf->DRAMb.bss,
				&elf->TCMb.bss, &elf->TCMb_local.bss,
				&elf->SFRw.bss);
		} else if (strncmp(".data.", shdr_name, 6) == 0) {
			const char *mem_name = shdr_name + 6;

			__dsp_elf32_get_mem(idx, mem_name, &elf->DMb.data,
				&elf->DMb_local.data, &elf->DRAMb.data,
				&elf->TCMb.data, &elf->TCMb_local.data,
				&elf->SFRw.data);
		} else if (strncmp(".rodata.", shdr_name, 8) == 0) {
			const char *mem_name = shdr_name + 8;

			__dsp_elf32_get_mem(idx, mem_name, &elf->DMb.rodata,
				&elf->DMb_local.rodata,
				&elf->DRAMb.rodata, &elf->TCMb.rodata,
				&elf->TCMb_local.rodata,
				&elf->SFRw.rodata);
		} else if (strncmp(".rela.", shdr_name, 6) == 0) {
			const char *sec_name = shdr_name + 6;

			ret = __dsp_elf32_get_mem_rela(idx, sec_name, elf,
					&elf->text.rela, &elf->DMb.rela,
					&elf->DMb_local.rela, &elf->DRAMb.rela,
					&elf->TCMb.rela, &elf->TCMb_local.rela,
					&elf->SFRw.rela);
			if (ret == -1) {
				DL_ERROR("Failed to get mem rela\n");
				return -1;
			}
		}
	}

	ret = __dsp_elf32_get_bss_symhash(elf);
	if (ret == -1) {
		DL_ERROR("Failed to get bss symhash\n");
		return -1;
	}

	ret = __dsp_elf32_get_extern_sym(elf);
	if (ret == -1) {
		DL_ERROR("Failed to get extern sym\n");
		return -1;
	}

	return 0;
}

static void __dsp_elf32_mem_free(struct dsp_elf32_mem *mem)
{
	dsp_list_free(&mem->robss, struct dsp_elf32_idx_node, node);
	dsp_list_free(&mem->bss, struct dsp_elf32_idx_node, node);
	dsp_list_free(&mem->rodata, struct dsp_elf32_idx_node, node);
	dsp_list_free(&mem->data, struct dsp_elf32_idx_node, node);
	dsp_list_free(&mem->rela, struct dsp_elf32_rela_node, node);
}

static void __dsp_elf32_text_free(struct dsp_elf32_text *text)
{
	dsp_list_free(&text->text, struct dsp_elf32_idx_node, node);
	dsp_list_free(&text->rela, struct dsp_elf32_rela_node, node);
}

void dsp_elf32_free(struct dsp_elf32 *elf)
{
	dsp_list_free(&elf->bss_sym, struct dsp_elf32_idx_node, node);
	dsp_list_free(&elf->extern_sym, struct dsp_elf32_idx_node, node);
	dsp_hash_free(&elf->symhash, 0);
	__dsp_elf32_mem_free(&elf->DMb);
	__dsp_elf32_mem_free(&elf->DMb_local);
	__dsp_elf32_mem_free(&elf->TCMb);
	__dsp_elf32_mem_free(&elf->TCMb_local);
	__dsp_elf32_mem_free(&elf->DRAMb);
	__dsp_elf32_mem_free(&elf->SFRw);
	__dsp_elf32_text_free(&elf->text);
}

static unsigned int __dsp_elf32_align_4byte(unsigned int size)
{
	return (size + 0x3) & ~(0x3);
}

static unsigned int __dsp_elf32_get_section_list_size(
	struct dsp_list_head *head, struct dsp_elf32 *elf)
{
	unsigned int total = 0;
	struct dsp_list_node *node;

	dsp_list_for_each(node, head) {
		struct dsp_elf32_idx_node *idx_node =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_shdr *sec = elf->shdr + idx_node->idx;
		unsigned int size = sec->sh_size;

		total += __dsp_elf32_align_4byte(size);
	}
	return total;
}

unsigned int dsp_elf32_get_text_size(struct dsp_elf32 *elf)
{
	return __dsp_elf32_get_section_list_size(&elf->text.text, elf);
}

unsigned int dsp_elf32_get_mem_size(struct dsp_elf32_mem *mem,
	struct dsp_elf32 *elf)
{
	unsigned int total = 0;

	total += __dsp_elf32_get_section_list_size(&mem->robss, elf);
	total += __dsp_elf32_get_section_list_size(&mem->rodata, elf);
	total += __dsp_elf32_get_section_list_size(&mem->bss, elf);
	total += __dsp_elf32_get_section_list_size(&mem->data, elf);
	return total;
}

void dsp_elf32_print(struct dsp_elf32 *elf)
{
	dsp_elf32_hdr_print(elf);
	dsp_elf32_shdr_print(elf);
	dsp_elf32_symtab_print(elf);
	dsp_elf32_bss_sym_print(elf);
	dsp_elf32_extern_sym_print(elf);
	dsp_elf32_symhash_print(elf);
	dsp_elf32_text_print(elf);
	dsp_elf32_DMb_print(elf);
	dsp_elf32_DMb_local_print(elf);
	dsp_elf32_DRAMb_print(elf);
	dsp_elf32_TCMb_print(elf);
	dsp_elf32_TCMb_local_print(elf);
	dsp_elf32_SFRw_print(elf);
}

void dsp_elf32_hdr_print(struct dsp_elf32 *elf)
{
	int idx;
	struct dsp_elf32_hdr *header = elf->hdr;

	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf header\n");

	DL_BUF_STR("Elf Magic number : ");

	for (idx = 0; idx < 16; idx++)
		DL_BUF_STR("%x ", header->e_ident[idx]);

	DL_BUF_STR("\n");
	DL_PRINT_BUF(DEBUG);

	DL_DEBUG("Class : %s\n", elf_class[header->e_ident[EI_CLASS]]);
	DL_DEBUG("Endian : %s\n", elf_endian[header->e_ident[EI_DATA]]);
	DL_DEBUG("Version : %d\n", header->e_ident[EI_VERSION]);
	DL_DEBUG("OS ABI : %s\n", elf_os[header->e_ident[EI_OSABI]]);
	DL_DEBUG("ABI VERSION : %d\n", header->e_ident[EI_ABIVERSION]);
	DL_DEBUG("OBJ Type : %s\n", elf_type[header->e_type]);
	DL_DEBUG("Machine id : %#x\n", header->e_machine);
	DL_DEBUG("Elf version : %d\n", header->e_version);
	DL_DEBUG("Entry point : %#x\n", header->e_entry);
	DL_DEBUG("Program header address : %#x\n", header->e_phoff);
	DL_DEBUG("Section header address : %#x\n", header->e_shoff);
	DL_DEBUG("Flag : %d\n", header->e_flags);
	DL_DEBUG("Header Size : %d\n", header->e_ehsize);
	DL_DEBUG("Program header size : %d\n", header->e_phentsize);
	DL_DEBUG("Program header entry : %d\n", header->e_phnum);
	DL_DEBUG("Section header size : %d\n", header->e_shentsize);
	DL_DEBUG("Section header entry : %d\n", header->e_shnum);
	DL_DEBUG("Section header string table index : %d\n",
		header->e_shstrndx);
}

void dsp_elf32_shdr_print(struct dsp_elf32 *elf)
{
	unsigned int idx;
	struct dsp_elf32_shdr *shdr = elf->shdr;
	char *shstrtab = elf->shstrtab;

	DL_DEBUG(DL_BORDER);
	DL_DEBUG("section header\n");
	for (idx = 0; idx < elf->shdr_num; idx++) {
		DL_BUF_STR("[%d] ", idx);
		DL_BUF_STR("%s ", shstrtab + shdr[idx].sh_name);
		DL_BUF_STR("type:%d ", shdr[idx].sh_type);
		DL_BUF_STR("flag:%#x ", shdr[idx].sh_flags);
		DL_BUF_STR("addr:%#x ", shdr[idx].sh_addr);
		DL_BUF_STR("off:%#x ", shdr[idx].sh_offset);
		DL_BUF_STR("sz:%d ", shdr[idx].sh_size);
		DL_BUF_STR("link:%d ", shdr[idx].sh_link);
		DL_BUF_STR("info:%d ", shdr[idx].sh_info);
		DL_BUF_STR("align:%d ", shdr[idx].sh_addralign);
		DL_BUF_STR("entsz:%d\n", shdr[idx].sh_entsize);
		DL_PRINT_BUF(DEBUG);
	}
}

static void __dsp_elf32_print_sym(struct dsp_elf32_sym *sym, char *strtab)
{
	DL_BUF_STR("%s ", strtab + sym->st_name);
	DL_BUF_STR("value:%#x ", sym->st_value);
	DL_BUF_STR("sz:%d ", sym->st_size);
	DL_BUF_STR("info:%d ", sym->st_info);
	DL_BUF_STR("other:%d ", sym->st_other);
	DL_BUF_STR("ndx:%d\n", sym->st_shndx);
	DL_PRINT_BUF(DEBUG);
}

void dsp_elf32_symtab_print(struct dsp_elf32 *elf)
{
	unsigned int idx;

	DL_DEBUG(DL_BORDER);
	DL_DEBUG("symbol table\n");

	for (idx = 0; idx < elf->symtab_num; idx++) {
		DL_BUF_STR("[%d]: ", idx);
		__dsp_elf32_print_sym(&elf->symtab[idx], elf->strtab);
	}
}

void dsp_elf32_bss_sym_print(struct dsp_elf32 *elf)
{
	struct dsp_list_node *node;

	DL_DEBUG(DL_BORDER);
	DL_DEBUG("bss symbols\n");
	dsp_list_for_each(node, &elf->bss_sym) {
		struct dsp_elf32_idx_node *symnode =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_sym *sym = &elf->symtab[symnode->idx];

		DL_BUF_STR("[bss]: ");
		__dsp_elf32_print_sym(sym, elf->strtab);
	}
}

void dsp_elf32_extern_sym_print(struct dsp_elf32 *elf)
{
	struct dsp_list_node *node;

	DL_DEBUG(DL_BORDER);
	DL_DEBUG("extern symbols\n");
	dsp_list_for_each(node, &elf->extern_sym) {
		struct dsp_elf32_idx_node *symnode =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_sym *sym = &elf->symtab[symnode->idx];

		DL_BUF_STR("[extern]: ");
		__dsp_elf32_print_sym(sym, elf->strtab);
	}
}

void dsp_elf32_symhash_print(struct dsp_elf32 *elf)
{
	int idx;
	struct dsp_list_node *node;

	DL_DEBUG(DL_BORDER);
	DL_DEBUG("symbol hash table\n");

	for (idx = 0; idx < DSP_HASH_MAX; idx++) {
		dsp_list_for_each(node, &elf->symhash.list[idx]) {
			struct dsp_hash_node *hash_node =
				container_of(node, struct dsp_hash_node, node);
			struct dsp_elf32_sym *sym =
				(struct dsp_elf32_sym *)hash_node->value;

			DL_BUF_STR("key[%u] ", hash_node->key);
			__dsp_elf32_print_sym(sym, elf->strtab);
		}
	}
}

static void __dsp_elf32_print_rela(struct dsp_list_head *rela_list,
	struct dsp_elf32 *elf)
{
	unsigned int idx;
	unsigned int r_info;
	struct dsp_elf32_rela *rela;
	struct dsp_list_node *node;
	struct dsp_elf32_rela_node *rela_node;

	dsp_list_for_each(node, rela_list) {
		rela_node = container_of(node,
				struct dsp_elf32_rela_node, node);

		DL_DEBUG(".rela[%d]\n", rela_node->idx);
		rela = rela_node->rela;

		for (idx = 0; idx < rela_node->rela_num; idx++) {
			r_info = rela[idx].r_info;

			DL_BUF_STR("[%d]: ", idx);
			DL_BUF_STR("offset:%d ", rela[idx].r_offset);
			DL_BUF_STR("info:%#x ", r_info);
			DL_BUF_STR("sym:%d(%s) ", r_info >> 8, elf->strtab +
				elf->symtab[r_info >> 8].st_name);
			DL_BUF_STR("reltype:%d ", r_info & 0xff);
			DL_BUF_STR("r_addend:%d\n", rela[idx].r_addend);
			DL_PRINT_BUF(DEBUG);
		}

		DL_DEBUG("\n");
	}
}

void dsp_elf32_text_print(struct dsp_elf32 *elf)
{
	unsigned int idx;
	int jdx;
	struct dsp_list_node *node;

	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf32 mem : text(%u)\n",
		dsp_elf32_get_text_size(elf));
	dsp_list_for_each(node, &elf->text.text) {
		struct dsp_elf32_idx_node *idx_node =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_shdr *text_hdr = elf->shdr + idx_node->idx;
		unsigned int *text = (unsigned int *)(elf->data +
				text_hdr->sh_offset);
		size_t size = __dsp_elf32_align_4byte(text_hdr->sh_size) /
			sizeof(unsigned int);

		DL_DEBUG(".text[%d]\n", idx_node->idx);
		for (idx = 0; idx < size; idx++) {
			if (idx != 0 && idx % 4 == 0) {
				DL_BUF_STR("\n");
				DL_PRINT_BUF(DEBUG);
			}

			DL_BUF_STR("0x");
			for (jdx = 0; jdx < 4; jdx++)
				DL_BUF_STR("%02x",
					((const unsigned char *)
						(text + idx))[jdx]);
			DL_BUF_STR(" ");
		}

		DL_BUF_STR("\n");
		DL_PRINT_BUF(DEBUG);
		DL_DEBUG("\n");
	}
	__dsp_elf32_print_rela(&elf->text.rela, elf);
}

static void __dsp_elf32_print_mem(struct dsp_elf32_mem *mem,
	struct dsp_elf32 *elf)
{
	unsigned int idx;
	struct dsp_list_node *node;

	dsp_list_for_each(node, &mem->robss) {
		struct dsp_elf32_idx_node *idx_node =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_shdr *bss_hdr = elf->shdr + idx_node->idx;

		DL_DEBUG("robss[%d] : %d\n", idx_node->idx, bss_hdr->sh_size);
	}

	dsp_list_for_each(node, &mem->rodata) {
		struct dsp_elf32_idx_node *idx_node =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_shdr *rodata_hdr = elf->shdr + idx_node->idx;
		unsigned int *rodata = (unsigned int *)(elf->data +
				rodata_hdr->sh_offset);
		size_t size = __dsp_elf32_align_4byte(rodata_hdr->sh_size) /
			sizeof(unsigned int);

		DL_DEBUG("\n");
		DL_DEBUG("rodata[%d]\n", idx_node->idx);
		for (idx = 0; idx < size; idx++) {
			if (idx != 0 && idx % 4 == 0) {
				DL_BUF_STR("\n");
				DL_PRINT_BUF(DEBUG);
			}

			DL_BUF_STR("0x%08x ", rodata[idx]);
		}

		DL_BUF_STR("\n");
		DL_PRINT_BUF(DEBUG);
	}

	dsp_list_for_each(node, &mem->bss) {
		struct dsp_elf32_idx_node *idx_node =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_shdr *bss_hdr = elf->shdr + idx_node->idx;

		DL_DEBUG("bss[%d] : %d\n", idx_node->idx, bss_hdr->sh_size);
	}

	dsp_list_for_each(node, &mem->data) {
		struct dsp_elf32_idx_node *idx_node =
			container_of(node, struct dsp_elf32_idx_node, node);
		struct dsp_elf32_shdr *data_hdr = elf->shdr + idx_node->idx;
		unsigned int *data = (unsigned int *)(elf->data +
				data_hdr->sh_offset);
		size_t size = __dsp_elf32_align_4byte(data_hdr->sh_size) /
			sizeof(unsigned int);

		DL_DEBUG("\n");
		DL_DEBUG("data[%d]\n", idx_node->idx);
		for (idx = 0; idx < size; idx++) {
			if (idx != 0 && idx % 4 == 0) {
				DL_BUF_STR("\n");
				DL_PRINT_BUF(DEBUG);
			}

			DL_BUF_STR("0x%08x ", data[idx]);
		}

		DL_BUF_STR("\n");
		DL_PRINT_BUF(DEBUG);
	}

	__dsp_elf32_print_rela(&mem->rela, elf);
}

void dsp_elf32_DMb_print(struct dsp_elf32 *elf)
{
	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf32 mem : DMb(%u)\n",
		dsp_elf32_get_mem_size(&elf->DMb, elf));
	__dsp_elf32_print_mem(&elf->DMb, elf);
}

void dsp_elf32_DMb_local_print(struct dsp_elf32 *elf)
{
	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf32 mem : DMb_local(%u)\n",
		dsp_elf32_get_mem_size(&elf->DMb_local, elf));
	__dsp_elf32_print_mem(&elf->DMb_local, elf);
}

void dsp_elf32_DRAMb_print(struct dsp_elf32 *elf)
{
	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf32 mem : DRAMb(%u)\n",
		dsp_elf32_get_mem_size(&elf->DRAMb, elf));
	__dsp_elf32_print_mem(&elf->DRAMb, elf);
}

void dsp_elf32_TCMb_print(struct dsp_elf32 *elf)
{
	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf32 mem : TCMb(%u)\n",
		dsp_elf32_get_mem_size(&elf->TCMb, elf));
	__dsp_elf32_print_mem(&elf->TCMb, elf);
}

void dsp_elf32_TCMb_local_print(struct dsp_elf32 *elf)
{
	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf32 mem : TCMb_local(%u)\n",
		dsp_elf32_get_mem_size(&elf->TCMb_local, elf));
	__dsp_elf32_print_mem(&elf->TCMb_local, elf);
}

void dsp_elf32_SFRw_print(struct dsp_elf32 *elf)
{
	DL_DEBUG(DL_BORDER);
	DL_DEBUG("Elf32 mem : SFRw(%u)\n",
		dsp_elf32_get_mem_size(&elf->SFRw, elf));
	__dsp_elf32_print_mem(&elf->SFRw, elf);
}

int dsp_elf32_load_libs(struct dsp_dl_lib_info *infos,
	struct dsp_lib **libs, int libs_size)
{
	int ret, idx;

	DL_DEBUG("load elf\n");

	for (idx = 0; idx < libs_size; idx++) {
		if (!libs[idx]->elf) {
			struct dsp_elf32 *elf;

			DL_DEBUG("Load ELF for library %s\n",
				libs[idx]->name);
			elf = (struct dsp_elf32 *)
				dsp_dl_malloc(sizeof(*elf), "Elf");
			__dsp_elf32_init(elf);
			libs[idx]->elf = elf;

			ret = dsp_elf32_load(libs[idx]->elf,
					&infos[idx].file);
			if (ret == -1) {
				DL_ERROR("CHK_ERR\n");
				return -1;
			}
		}
	}

	return 0;
}